如何获得小数点符号?
我想知道设备上设置的小数符号是哪个。到目前为止,我一直在使用这种方法:直到现在
NSString *decimalSymbol;
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
[f setMinimumFractionDigits:2];
[f setMaximumFractionDigits:2];
[f setGroupingSeparator:@" "];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];
[formatter setGroupingSeparator:@" "];
NSRange range = {1,1};
decimalSymbol = [[formatter stringFromNumber:[f numberFromString:[@"" stringByAppendingFormat:@"%.02f", 1.0f]]] substringWithRange:range];
[formatter release];
[f release];
,当我在另一台设备(4.3)上进行测试时,它都工作正常 - 返回 null
。 可能是什么问题?
还有其他方法来检索小数点符号吗?
稍后编辑:
我可以使用:
decimalSymbol = [[@"" stringByAppendingFormat:@"%.02f", 1.0f] substringWithRange:range];
但为什么其他方式在这个特定设备上不起作用?
I want to get which is the decimal symbol set up on the device. Until now I was using this method:
NSString *decimalSymbol;
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
[f setMinimumFractionDigits:2];
[f setMaximumFractionDigits:2];
[f setGroupingSeparator:@" "];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];
[formatter setGroupingSeparator:@" "];
NSRange range = {1,1};
decimalSymbol = [[formatter stringFromNumber:[f numberFromString:[@"" stringByAppendingFormat:@"%.02f", 1.0f]]] substringWithRange:range];
[formatter release];
[f release];
It was working fine until now when I am testing on another device (4.3) - null
is returned.
What could be the problem?
Is there another way to retrive the decimal symbol?
LATER EDIT:
I can use:
decimalSymbol = [[@"" stringByAppendingFormat:@"%.02f", 1.0f] substringWithRange:range];
but why the other way it doe not work on this particular device?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好的方法是从当前区域设置获取小数分隔符,而不初始化任何
NSNumberFormatter
对象。像这样:
The best way is get the decimal separator from the current locale without init any
NSNumberFormatter
object.Like this:
斯威夫特 4 版本:
Swift 4 version:
Swift 4
您可以使用
Locale.current.decimalSeparator
或NSLocale.current.decimalSeparator
。和
NSLocale Apple 文档
Swift 4
You can use
Locale.current.decimalSeparator
orNSLocale.current.decimalSeparator
.and
NSLocale Apple doc
decimalSeparator
返回一个字符串,其中包含接收者用来表示小数点分隔符的字符。
- (NSString *)decimalSeparator
示例:
decimalSymbol = [fdecimalSeparator];
decimalSeparator
Returns a string containing the character the receiver uses to represent decimal separators.
- (NSString *)decimalSeparator
Example:
decimalSymbol = [f decimalSeparator];