关于如何清除小数点后面多余的0

金额的 呈现一般出现11.00 , 11.10, 这种情况 需要去除多余的0还有小数点,直接上代码

-(NSString *)removeFloatAllZero:(NSString *)string

{


   
NSString * testNumber = [NSString stringWithFormat:@"%.2f",string.doubleValue];

    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.doubleValue)];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

    formatter.numberStyle = kCFNumberFormatterDecimalStyle;

    NSString *formatterString = [formatter stringFromNumber:[NSNumber numberWithDouble:[outNumber doubleValue]]];

    NSRange range = [formatterString rangeOfString:@"."]; //现获取要截取的字符串位置

    if (range.length>0) {

        NSString * result = [formatterString substringFromIndex:range.location]; //截取字符串

        

        if (result.length>=4) {

            

            formatterString=[formatterString substringToIndex:formatterString.length-1];

        }

    }

    NSLog(@"Formatted number string:%@",formatterString);

    NSLog(@"Formatted number string:%@",outNumber);

    NSString *formatterAppend = nil;

    if ([formatterString containsString:@","]) {//当金额过大会出现123,4.11的情况 ,去掉逗号

       NSArray *formatterArr =   [formatterString componentsSeparatedByString:@","];

      formatterAppend=  [formatterArr componentsJoinedByString:@""];

        return formatterAppend;

    }else{

        return formatterString;

    }

    return nil;

}



你可能感兴趣的:(iOS)