代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"--float-%@-",[self stringDisposeWithFloat:9.000899990000]); } //浮点数处理并去掉多余的0 -(NSString *)stringDisposeWithFloat:(float)floatValue { NSString *str = [NSString stringWithFormat:@"%f",floatValue]; int len = str.length; for (int i = 0; i < len; i++) { if (![str hasSuffix:@"0"]) break; else str = [str substringToIndex:[str length]-1]; } if ([str hasSuffix:@"."])//避免像2.0000这样的被解析成2. { return [str substringToIndex:[str length]-1];//s.substring(0, len - i - 1); } else { return str; } }
输出:
2014-12-02 17:01:08.253 浮点数处理并去掉多余的0[21460:60b] --float-9.0009-