在float中删除小数点前的前导零

时间:2013-09-23 05:40:17

标签: ios objective-c xcode

我试图删除xcode中两位小数位浮点数的前导零。

arrFraction = [[NSMutableArray alloc] init];
for (float fraction = .00; fraction <=.99; fraction+= .01) {
    NSString *fractionString = [NSString stringWithFormat:@"%.2f%", fraction];
    [arrFraction addObject:fractionString];
}

这导致NSMutableArray的值为0.00,但0.99

我想要的是.00.99

1 个答案:

答案 0 :(得分:3)

尝试这样,

    NSString *str =@"0000.00013200";
    NSRange range = [str rangeOfString:@"^0*" options:NSRegularExpressionSearch];
    str= [str stringByReplacingCharactersInRange:range withString:@""];
    NSLog(@"%@",str);

O / P: - .00013200