agim
agim

Reputation: 23

Remove leading zero before decimal in a float

I'm trying to remove the leading zero on a two decimal place float in xcode.

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

This results in a NSMutableArray with values of 0.00 though 0.99

What I would like is .00 through .99

Upvotes: 2

Views: 1157

Answers (1)

Balu
Balu

Reputation: 8460

try like this,

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

O/P:-.00013200

Upvotes: 4

Related Questions