开发者

Which objective-c type is appropriate for handling money?

开发者 https://www.devze.com 2023-03-12 12:23 出处:网络
Which objective-c type is appropriate for handling money? I need something开发者_StackOverflow which is Core Data compatible. There are two solutions:

Which objective-c type is appropriate for handling money? I need something开发者_StackOverflow which is Core Data compatible.


There are two solutions:

  • 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.

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).

I'd recommend solution #1, but go with whichever of those you think would work best.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号