2

I have a function method in class file. like this:

-(NSArray*)getPayMoney:(NSUInteger)money month:(NSInteger)month rates:(float)rates;

when I transfer the rates value 0.06 from this, in debug mode get rates is 0.059999. Does anyone knows the reason?

1
  • 1
    If you are working with real money and want high precision then you should use NSDecimalNumber. We, as users, tend to get pissed when float precision causes us to lose money, even if it's just cents ;) Oct 30, 2013 at 9:25

1 Answer 1

0

This is fundamental Computer Science - floating point representation simply cannot represent all decimal numbers accurately. Please do some reading:

The bottom line: if you are working with currency values, do not use floating point types. You will have two major problems: first, you cannot always accurately convert currency values to floating point and back, and second, you could have rounding errors that give incorrect results in certain calculations.

You must use a BCD or decimal type to calculate with currency. Apple provides direct support for this in Cocoa:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.