0

如何准确地float value四舍五入。1.449567

输出值应为小数点浮点值

输出应该是这样的:

for 1 decimal point  **f=1.5**
for 2 decimal point  **f=1.45**
for 3 decimal point  **f=1.450**
for 4 decimal point  **f=1.4496**
4

1 回答 1

1

我建议考虑使用NSNumberFormatter

例子:

float roundedValue =1.45999f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
于 2013-04-25T14:22:13.653 回答