View Full Version : NSDecimalNumber doubleValue -- conversion precision?

Aug 31, 2008, 04:20 PM
I'm getting very small conversion errors from doubleValue when the receiver is an NSDecimalNumber or an NSString... for example:

NSDecimalNumber *dNumber = [NSDecimalNumber decimalNumberWithString:tmpString];
double tmpValue = [dNumber doubleValue];

If the input string (tmpString) is "3.4", the output of tmpValue is 3.399999999999

Is there anything that can be done? NSScanner does the same thing, other input values have precision errors where the precision of a double comes into play, but I would not have expected these results.

Any insight is appreciated!!!

Apr 17, 2014, 11:18 PM
I know this is a really old thread, but I just ran into this as well. The only thing I've found so far that works is to use a number formatter and a string as an intermediary:

NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:@"3.4];
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [nf stringFromNumber:dn];
double dv = [string doubleValue]; // Should be 3.4

Apr 18, 2014, 08:57 AM
It's not possible as digital floating point numbers can't represent many numbers from the decimal system. If you need exacty values you have to use something like fixed point numbers, i.e. use integers to store cent instead of dollars.

Apr 18, 2014, 12:19 PM
Yeah, I know. SKProduct uses NSDecimalNumber, and I have a 3rd-party API that expects the price as a double. (sigh) Just doing the best I can. What I've got seems to work better than -[NSDecimalNumber doubleValue].