Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tawpie

macrumors newbie
Original poster
Jul 8, 2008
18
0
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!!!
 
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
 
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.
 
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].
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.