添加千分符

 对于理财,借款类APP来说,没有比千分位格式化来显示某些数据更有说明力了,最近刚好用到这个东西,就说一下吧。
  • (NSString *)thousandSeparatWith:(NSString *)string{
    if (string.length<=0 || [string isEqualToString:@"(null)"]) {//判断值是否存在
    return @"0.00";
    }else if (string.floatValue < 1000) {//如果小于1000则不需要添加千分符
    return string;
    }else{
    NSString *integerString = @"";
    NSString *decimalString = @"00";
    if ([string rangeOfString:@"."].location != NSNotFound) {
    //带小数的情况下需要根据小数点切分,然后再添加千分符
    integerString = [[string componentsSeparatedByString:@"."] firstObject];
    decimalString = [[string componentsSeparatedByString:@"."] lastObject];
    }else{
    integerString = [[string componentsSeparatedByString:@"."] firstObject];
    }
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@",###"];
    NSString * lastString = [NSString stringWithFormat:@"%@.%@",[numberFormatter stringFromNumber:[NSNumber numberWithDouble:integerString.doubleValue]],decimalString];

    return lastString;
    

    }
    return @"";
    }

推荐阅读更多精彩内容