Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which objective-c type is appropriate for handling money?

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

like image 235
Moshe Avatar asked Jun 14 '11 04:06

Moshe


People also ask

What can I use for money in c#?

The best datatype to use for currency in C# is decimal. The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.


1 Answers

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.

like image 140
Adam Rosenfield Avatar answered Oct 03 '22 20:10

Adam Rosenfield