从特定于语言环境的字符串获取NSDecimalNumber? [英] Obtaining an NSDecimalNumber from a locale specific string?

查看:174
本文介绍了从特定于语言环境的字符串获取NSDecimalNumber?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字符串是特定于语言环境(例如,0.01或0,01)。我想把这个字符串转换为NSDecimalNumber。从示例中,我已经在interwebs上看到了 a>,这是通过使用NSNumberFormatter a la实现的:

I have some string s that is locale specific (eg, 0.01 or 0,01). I want to convert this string to a NSDecimalNumber. From the examples I've seen thus far on the interwebs, this is accomplished by using an NSNumberFormatter a la:

NSString *s = @"0.07";

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGeneratesDecimalNumbers:YES];
NSDecimalNumber *decimalNumber = [formatter numberFromString:s];

NSLog([decimalNumber stringValue]); // prints 0.07000000000000001

我使用的是10.4模式也是iPhone上唯一可用的模式),但向格式器指示我想生成十进制数。注意,我已经简化了我的例子(我实际上是处理货币字符串)。然而,我显然做错了,它返回一个值,说明浮点数的不精确。

I'm using 10.4 mode (in addition to being recommended per the documentation, it is also the only mode available on the iPhone) but indicating to the formatter that I want to generate decimal numbers. Note that I've simplified my example (I'm actually dealing with currency strings). However, I'm obviously doing something wrong for it to return a value that illustrates the imprecision of floating point numbers.

转换一个区域的特定数字的正确方法是什么string to an NSDecimalNumber?

What is the correct method to convert a locale specific number string to an NSDecimalNumber?

编辑:请注意,我的例子是为了简单。我问的问题也应该涉及当你需要采取一个特定于语言环境的货币字符串并将其转换为NSDecimalNumber。此外,这可以扩展为特定于语言环境的百分比字符串并将其转换为NSDecimalNumber。

Note that my example is for simplicity. The question I'm asking also should relate to when you need to take a locale specific currency string and convert it to an NSDecimalNumber. Additionally, this can be expanded to a locale specific percentage string and convert it to a NSDecimalNumber.

推荐答案

基于Boaz Stuller的答案,我为这个问题记录了一个bug给苹果。直到解决,这里是我决定作为最好的方法采取的解决方法。这些解决方法仅仅依赖于将十进制数舍入到适当的精度,这是一个可以补充现有代码(而不是从格式化程序切换到扫描程序)的简单方法。

Based on Boaz Stuller's answer, I logged a bug to Apple for this issue. Until that is resolved, here are the workarounds I've decided upon as being the best approach to take. These workarounds simply rely upon rounding the decimal number to the appropriate precision, which is a simple approach that can supplement your existing code (rather than switching from formatters to scanners).

一般数字

基本上,我只是根据对我的情况有意义的规则,将数字四舍五入。所以,YMMV取决于你支持的精度。

Essentially, I'm just rounding the number based on rules that make sense for my situation. So, YMMV depending on the precision you support.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGeneratesDecimalNumbers:TRUE];

NSString *s = @"0.07";

// Create your desired rounding behavior that is appropriate for your situation
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE]; 

NSDecimalNumber *decimalNumber = [formatter numberFromString:s];
NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

NSLog([decimalNumber stringValue]); // prints 0.07000000000000001
NSLog([roundedDecimalNumber stringValue]); // prints 0.07

货币

处理货币(这是我试图解决的实际问题)只是处理一般数字的一个微小的变化。关键是舍入行为的规模是由语言区域货币使用的最大小数位数决定的。

Handling currencies (which is the actual problem I'm trying to solve) is just a slight variation on handling general numbers. The key is that the scale of the rounding behavior is determined by the maximum fractional digits used by the locale's currency.

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyFormatter setGeneratesDecimalNumbers:TRUE];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

// Here is the key: use the maximum fractional digits of the currency as the scale
int currencyScale = [currencyFormatter maximumFractionDigits];

NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:currencyScale raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE]; 

// image s is some locale specific currency string (eg, $0.07 or €0.07)
NSDecimalNumber *decimalNumber = (NSDecimalNumber*)[currencyFormatter numberFromString:s];
NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

NSLog([decimalNumber stringValue]); // prints 0.07000000000000001
NSLog([roundedDecimalNumber stringValue]); // prints 0.07

这篇关于从特定于语言环境的字符串获取NSDecimalNumber?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆