iOS - 删除浮点型数字多余的0

2016-05-17  本文已影响343人  Cdream

删除小数点后面多余的0,比如90.00100可以转换成90.001

//删除小数点后面多余的0
-(NSString *)changeFloat:(NSString *)stringFloat
{
    NSInteger length = [stringFloat length];
    if ([stringFloat containsString:@"."]) {
        
        for(NSInteger i = length - 1; i >= 0; i--)
        {
            NSString *subString = [stringFloat substringFromIndex:i];
            if(![subString isEqualToString:@"0"])
            {
                if ([subString isEqualToString:@"."]) {
                    
                    return [stringFloat substringToIndex:[stringFloat length] - 1];
                    
                }else{
                    
                    return stringFloat;
                }
            }
            else
            {
                stringFloat = [stringFloat substringToIndex:i];
            }
        }
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读