// 这是保留2位小数,并且不会四舍五入
NSNumber *number = @(1.988888);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"###0.00"];
formatter.maximumFractionDigits = 2;
formatter.roundingMode = NSNumberFormatterRoundDown;
NSLog(@"%@", [formatter stringFromNumber:number]);
// 这是保留1位小数,并且不会四舍五入
NSNumber *number = @(1.988888);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"###0.0"];
formatter.maximumFractionDigits = 1;
formatter.roundingMode = NSNumberFormatterRoundDown;
NSLog(@"%@", [formatter stringFromNumber:number]);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)