# NSDecimalNumber doubleValue -- conversion precision?

Discussion in 'Mac Programming' started by tawpie, Aug 31, 2008.

1. ### tawpie macrumors newbie

Joined:
Jul 8, 2008
#1
I'm getting very small conversion errors from doubleValue when the receiver is an NSDecimalNumber or an NSString... for example:

Code:
```	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!!!

2. ### dreese macrumors newbie

Joined:
Apr 17, 2014
#2
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:

Code:
```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
```

3. ### Dranix macrumors 6502a

Joined:
Feb 26, 2011
Location:
Gelnhausen, Germany
#3
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.

4. ### dreese macrumors newbie

Joined:
Apr 17, 2014
#4
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].