NSDecimal vs Float (iOS)

HM1 picture HM1 · Jan 10, 2011 · Viewed 8.4k times · Source

I've come across some blogs and forum posts where people say one should use NSDecimalNumber instead of floats when dealing with currency. I find I have to convert from NSDecimalNumber to Floats/Doubles in order to do some arithmetic which in my mind negates the usefulness of NSDecimalNumber. I mean why not just use floats and not bother with all the conversions hassles?

What's the benefit of NSDecimalNumber and how do you perform arithmetic on it?

What is this error thing that floats have and NSDecimalNumber does not which is why people say one MUST use NSDecimalNumber when dealing with currencies? Please quantify the error?

Thanks in advance,
Hiren.

Answer

Matt K picture Matt K · Jan 10, 2011

The class documentation has arithmetic instructions. The class is immutable so it looks a little funny. This question has very good answers and the link in the accepted answer is key.