给一个金额字符串插入逗号分隔 保留两位有效数字

  格式:###,###.##
 *
 *  @param number 金额字符串
 *
 *  @return
 */
- (NSString*) formatAmount:(NSString*)number
{
    NSMutableString *resultStr = [NSMutableString stringWithFormat:@"%.2f",[number doubleValue]];
    
    BOOL bellowZearo = NO;
    if ([number doubleValue]<0)
    {
        bellowZearo = YES;
        [resultStr replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
    }
    int count = ([resultStr length]-1)/3-2;
    int mod = [resultStr length]%3==0?3:[resultStr length]%3;
    
    for (int i=0; i<=count; i++)
    {
        [resultStr insertString:@"," atIndex:mod+3*(count-i)];
    }
    
    if (bellowZearo)
    {
        [resultStr insertString:@"-" atIndex:0];
    }
    
    return resultStr;
}

你可能感兴趣的:(给一个金额字符串插入逗号分隔 保留两位有效数字)