我应该使用NSDecimalNumber处理钱? [英] Should I use NSDecimalNumber to deal with money?

查看:96
本文介绍了我应该使用NSDecimalNumber处理钱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始编码我的第一个应用程序我使用NSNumber的金钱价值观,没有两次思考。然后我认为也许c类型足以处理我的价值观。然而,我被告知在iPhone SDK论坛使用NSDecimalNumber,因为它优秀的舍入能力。

As I started coding my first app I used NSNumber for money values without thinking twice. Then I thought that maybe c types were enough to deal with my values. Yet, I was advised in the iPhone SDK forum to use NSDecimalNumber, because of its excellent rounding capabilities.

不是气质的数学家,我认为尾数/指数范式可能是过度的;仍然,googlin周围,我意识到,大多数关于可可货币/货币的谈话被称为NSDecimalNumber。

Not being a mathematician by temperament, I thought that the mantissa/exponent paradigm might be overkill; still, googlin' around, I realised that most talks about money/currency in cocoa were referred to NSDecimalNumber.

请注意,我正在开发的应用程序将国际化,所以选择计数金额是不可行的,因为货币结构很大程度上取决于所使用的语言环境。

Notice that the app I am working on is going to be internationalised, so the option of counting the amount in cents is not really viable, for the monetary structure depends greatly on the locale used.

我90%去NSDecimalNumber,但由于我发现在网络上没有明确的答案(类似:如果你处理钱,使用NSDecimalNumber!)我想我会问这里。也许答案是显而易见的大多数,但我想确定之前开始一个大规模重新分解我的应用程序。

I am 90% sure that I need to go with NSDecimalNumber, but since I found no unambiguous answer on the web (something like: "if you deal with money, use NSDecimalNumber!") I thought I'd ask here. Maybe the answer is obvious to most, but I want to be sure before starting a massive re-factoring of my app.

说服我:)

推荐答案

Marcus Zarra stance on this:如果你正在处理货币,那么你应该使用NSDecimalNumber。他的文章激励我看看NSDecimalNumber,我已经非常深刻的印象。 IEEE浮点错误在处理base-10数学时已经使我烦恼了一会儿(1 *(0.5 - 0.4 - 0.1)= -0.00000000000000002776)和NSDecimalNumber抛弃它们。

Marcus Zarra has a pretty clear stance on this: "If you are dealing with currency at all, then you should be using NSDecimalNumber." His article inspired me to look into NSDecimalNumber, and I've been very impressed with it. IEEE floating point errors when dealing with base-10 math have been irritating me for a while (1 * (0.5 - 0.4 - 0.1) = -0.00000000000000002776) and NSDecimalNumber does away with them.

NSDecimalNumber不仅仅增加了二进制浮点精度的几个数字,它实际上做了base-10数学运算。这会删除像上面例子中显示的错误。

NSDecimalNumber doesn't just add another few digits of binary floating point precision, it actually does base-10 math. This gets rid of the errors like the one shown in the example above.

现在,我正在写一个符号数学应用程序,所以我希望30+十进制数精度没有奇怪的浮点错误可能是一个例外,但我认为值得一看。这些操作比简单的var = 1 + 2风格数学有点尴尬,但它们仍然是可管理的。如果你担心在数学操作期间分配所有类型的实例,NSDecimal是等效于NSDecimalNumber的C结构体,并且有C函数用于对其进行完全相同的数学运算。在我的经验中,除了最苛刻的应用程序(3,344,593添加/ s,MacBook Air上的254,017个分区/秒,281,555添加/秒,在iPhone上为12,027个分区/秒)之外,这些都是快速的。

Now, I'm writing a symbolic math application, so my desire for 30+ decimal digit precision and no weird floating point errors might be an exception, but I think it's worth looking at. The operations are a little more awkward than simple var = 1 + 2 style math, but they're still manageable. If you're worried about allocating all sorts of instances during your math operations, NSDecimal is the C struct equivalent of NSDecimalNumber and there are C functions for doing the exact same math operations with it. In my experience, these are plenty fast for all but the most demanding applications (3,344,593 additions/s, 254,017 divisions/s on a MacBook Air, 281,555 additions/s, 12,027 divisions/s on an iPhone).

另外一个好处是,NSDecimalNumber的descriptionWithLocale:方法提供了一个带有本地化版本号的字符串,包括正确的小数分隔符。对于其initWithString:locale:方法也是如此。

As an added bonus, NSDecimalNumber's descriptionWithLocale: method provides a string with a localized version of the number, including the correct decimal separator. The same goes in reverse for its initWithString:locale: method.

这篇关于我应该使用NSDecimalNumber处理钱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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