NSDecimalNumber doubleValue -- conversion precision?

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

  1. tawpie macrumors newbie

    Jul 8, 2008
    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!!!
  2. dreese macrumors newbie

    Apr 17, 2014
    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
  3. Dranix macrumors 6502a


    Feb 26, 2011
    Gelnhausen, Germany
    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

    Apr 17, 2014
    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].

Share This Page