哪种Objective-C类型适合处理资金? [英] Which objective-c type is appropriate for handling money?
本文介绍了哪种Objective-C类型适合处理资金?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪种objective-c类型适合处理货币?我需要与Core Data兼容的东西。
Which objective-c type is appropriate for handling money? I need something which is Core Data compatible.
推荐答案
有两种解决方案:
- 使用
int
,并始终以美分(或所用任何货币的最小除法)跟踪货币价值。仅使用整数计算。 - 使用
NSDecimalNumber
,它执行精确的十进制算术。
- Use an
int
, and always keep track of monetary values in cents (or the smallest possible division of whatever currency you're using). Use only integer calculations. - Use
NSDecimalNumber
, which does exact decimal arithmetic.
解决方案#1要求您在输入或输出货币值时都在美分和美元之间进行转换,而解决方案#2可能更难以编写代码(例如,您必须编写类似 [num1 decimalNumberByAdding:num2]
而不是 num1 + num2
来添加两个数字)。
Solution #1 requires you to convert between cents and dollars whenever you do input or output of monetary values, whereas solution #2 can be messier to code (e.g. you have to write something like [num1 decimalNumberByAdding:num2]
instead of num1 + num2
to add two numbers).
我建议使用解决方案#1,但是请选择您认为最有效的解决方案。
I'd recommend solution #1, but go with whichever of those you think would work best.
这篇关于哪种Objective-C类型适合处理资金?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文