I realize there have been other questions like this, but my problem does not seem to relate accurately. Here’s the deal.
I have an app, that does a calculation. I am using floats for this. All floats work fine, and I convert the float value into a NSNumber for formatting.
NSNumberFormatter *numFormatter = [NSNumberFormatter alloc];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numFormatter setMinimumFractionDigits:1];
[numFormatter setMaximumFractionDigits:5];
NSNumber *insulinRatio = [NSNumber numberWithFloat:insulinRatioCalc];
NSLog(@"Original Number = %@", insulinRatio);
My NSNumber, insulinRatio, has accurate precision. However, when I convert that into an NSString with NSNumberFormatter, it loses all decimal precision.
NSString *numberAsString = [numFormatter stringFromNumber:insulinRatio];
NSLog(@"Number = %@", numberAsString);
My output for these last logs where the following:
2013-01-08 16:12:27.944 MyApp[84062:c07] Original Number = 1.2
2013-01-08 16:12:27.944 MyApp[84062:c07] Number = 1
Why am I losing this precision. I know that NSNumber converts to double, but the NSNumber is fine here. It is the NSNumberFormatter. What am I doing wrong?
Thanks
Nevermind. I just had to initialize NSNumberFormatter. #fail moment.
solved the problem. 🙂