删除超过2个尾随零

Lun*_*ayo 2 iphone precision objective-c string-formatting

我已经在堆栈溢出中读了很多问题,我想要的是删除小数后面的两个或两个以上的尾随零.即:

12.00 ==> 12
12.30 ==> 12.30
12.35 ==> 12.35
12.345678 ==> 12.34
Run Code Online (Sandbox Code Playgroud)

Dan*_*oad 9

NSNumberFormatter *twoDecimalPlacesFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[twoDecimalPlacesFormatter setMaximumFractionDigits:2];
[twoDecimalPlacesFormatter setMinimumFractionDigits:0];

return [twoDecimalPlacesFormatter stringFromNumber:number];
Run Code Online (Sandbox Code Playgroud)