NSNumberFormatter保留小数点分隔符,即使没有小数存在
问题描述:
我有一个UILabel和格式化:NSNumberFormatter保留小数点分隔符,即使没有小数存在
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.”?
答
如果你想看到逗号的时候没有小数点,设置相应alwaysShowsDecimalSeparator
[formatter setAlwaysShowsDecimalSeparator:YES];
因此,您UILabel
会说 “12”(而不是 “12”)。