保留四位小数

保留四位小数 记录一下

+ (NSString*)decimalNumberWithDouble4:(NSString *)stringNumber{


    stringNumber =[NSString stringWithFormat:@"%@",stringNumber];


    if ([stringNumber rangeOfString:@"."].location ==NSNotFound) {

        NSString *string_com = [NSString stringWithFormat:@"%@.0000",stringNumber];

        return string_com;

    }else{

        NSArray*arays = [stringNumber componentsSeparatedByString:@"."];

        NSString*s_f = [arays objectAtIndex:0];

        NSString*s_e = [arays objectAtIndex:1];

if(s_e.length>4) {

            s_e = [s_e substringWithRange:NSMakeRange(0,4)];

        }else if(s_e.length==1){

            s_e = [NSString stringWithFormat:@"%@000",s_e];

        }else if(s_e.length==2){

            s_e = [NSString stringWithFormat:@"%@00",s_e];

        }else if(s_e.length==3){

            s_e = [NSString stringWithFormat:@"%@0",s_e];

        }


        NSString *string_combine = [NSString stringWithFormat:@"%@.%@",s_f,s_e];


        return string_combine;

    }


    return@"";

}

你可能感兴趣的:(保留四位小数)