Which objective-c type is appropriate for handling money? I need something which is Core Data compatible.
Which objective-c type is appropriate for handling money? I need something which is Core
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two solutions:
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.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 ofnum1 + num2to add two numbers).I’d recommend solution #1, but go with whichever of those you think would work best.