I have an array of strings number with decimal digits, that should be converted in NSNumber:
我有一个带十进制数字的字符串数组,应该在NSNumber中转换:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setDecimalSeparator:@"."];
NSArray * ar = @[@"0.0001",@"0.0010",@"0.002", @"0.02"];
for (NSString * numS in ar) {
NSNumber * num = [formatter numberFromString:numS];
NSLog(@"%@", num);
}
The log prints:
0.0001, 0.001, 2, 0.02
I can't understand why this is happening, I've also tried to change fraction digits properties with no success.
When I write a number with 3 fraction digits where the first two are zero, I always get an integer as result.
I'm using the simulator, Xcode 8.
We had run further tests:
日志打印:0.0001,0.001,2,0.02我无法理解为什么会发生这种情况,我也试图改变小数位数属性但没有成功。当我写一个带有3个小数位的数字,其中前两个为零时,我总是得到一个整数作为结果。我正在使用模拟器,Xcode 8.我们已经进行了进一步的测试:
- If we remove the style, it works as expected
- 如果我们删除样式,它会按预期工作
- The problem exists also on devices
- 设备上也存在问题
- In playground with swift 3 and the style, it works
- 在快速3的游乐场和风格,它的工作原理
[UPDATE]
[UPDATE]
I forgot to mention that my device locale is in italian, if I set it in english it works.
Probably set a style and later modify it's decimal separator when the official locale decimal separator doesn't match is not a good idea.
I still do not understand why this only happens for that specific format.
我忘了提到我的设备区域设置是意大利语,如果我用英语设置它可以工作。当官方区域设置小数分隔符不匹配时,可能设置样式并稍后修改它的小数分隔符不是一个好主意。我仍然不明白为什么这只发生在那种特定的格式上。
1 个解决方案
#1
1
In Italian locale, the .
is the grouping separator. That should basically tell you everything.
在意大利语中,。是分组分隔符。那应该基本上告诉你一切。
The advice is, set the locale, don't tamper with the specific language settings.
建议是,设置区域设置,不要篡改特定的语言设置。
#1
1
In Italian locale, the .
is the grouping separator. That should basically tell you everything.
在意大利语中,。是分组分隔符。那应该基本上告诉你一切。
The advice is, set the locale, don't tamper with the specific language settings.
建议是,设置区域设置,不要篡改特定的语言设置。