2013-09-23 78 views
1

我想刪除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

+0

可能重複的[從字符串中刪除第一個字符如果0](http://stackoverflow.com/questions/14031603/remove-first-character-from-string-if-0) – Kevin

+0

可能重複[C - 如何隱藏在printf中導致零](http://stackoverflow.com/questions/2595738/c-how-to-hide-leading-zero-in-printf) – Sebastian

回答

3

嘗試這樣,

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

O/P: - .00013200