即使没有小数,NSNumberFormatter也会保留小数点分隔符

时间:2013-08-27 16:02:49

标签: ios nsstring uilabel decimal nsformatter

我有一个UILabel及其格式化程序:

 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

[formatter setGroupingSeparator:@"."];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];
[formatter setDecimalSeparator:@","];

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:12.]]];

这样我的formattedString返回12

如果我想在没有小数的情况下保留逗号怎么办?所以我的UIlabel打印出“12”。

1 个答案:

答案 0 :(得分:0)

如果您想在没有小数时看到逗号,请相应地设置alwaysShowsDecimalSeparator

[formatter setAlwaysShowsDecimalSeparator:YES];

因此,您的UILabel会说“12”,而不是“12”。