2
我使用NSNumberFormatter
来格式化NSDecimalNumber
。如何配置NSNumberFormatter以显示不带前导零的小数部分?
我可以使用什么配置将0.012676
显示为1
?
重要:我不试图围捕1.0 - 我试图四舍五入到上百元发生在十进制,然后剥离前的零和小数点分隔符。
我目前的配置看起来是这样的:
// in a method...
NSDecimalNumber *value = /* retrieve a value like 0.012676 */;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
// this is the rounding behavior I want
[nf setRoundingMode: NSNumberFormatterRoundHalfEven];
[nf setMinimumFractionDigits: 2];
[nf setMaximumFractionDigits: 2];
return [nf stringFromNumber: value];
产生的输出:
.01
与感谢dasblinkenlight和Twitter的朋友时,解决方案只是乘以100
,然后消除小数位数:
[nf setMultiplier: [NSNumber numberWithInt: 100]];
[nf setMinimumFractionDigits: 0];
[nf setMaximumFractionDigits: 0];
谢谢! Twitter上也指出了这一点,所以我想在这里指出未来的访问者:https://twitter.com/photar/status/225233393804251136 – 2012-07-17 14:31:43