美文网首页
金额3位逗号分隔

金额3位逗号分隔

作者: iOS乐乐 | 来源:发表于2019-08-29 17:31 被阅读0次
    - (NSString *)formatToTwoDecimal:(id)count {
        NSString *originNumber;
        if ([count isKindOfClass:[NSString class]] || [count isKindOfClass:[NSNumber class]]) {
            NSInteger i = [count integerValue];
            originNumber = [NSString stringWithFormat:@"%ld",(long)I];
        } else {
            return @"¥0.00";
        }
        NSDecimalNumber *one = [NSDecimalNumber decimalNumberWithString:originNumber];
        NSDecimalNumber *two = [NSDecimalNumber decimalNumberWithString:@"100"];
        NSDecimalNumber *thr = [one decimalNumberByDividingBy:two];
        
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
        formatter.positiveFormat = @",###.##";
        NSString *money = [formatter stringFromNumber:[NSNumber numberWithDouble:[thr doubleValue]]];
        
        NSString *result = [NSString stringWithFormat:@"¥%@",money];
        
        if (![result containsString:@"."]) {  //被整除的情况
            result = [NSString stringWithFormat:@"%@.00",result];
        } else {                              //小数不足两位
            NSArray *array = [result componentsSeparatedByString:@"."];
            NSString *subNumber = array.lastObject;
            if (subNumber.length == 1) {
                result = [NSString stringWithFormat:@"%@.%@0",array.firstObject, array.lastObject];
            }
        }
        return result;
    }
    
    1235875-b9f5976682eb328a.png

    相关文章

      网友评论

          本文标题:金额3位逗号分隔

          本文链接:https://www.haomeiwen.com/subject/xoqwectx.html