-1

Explanation Why this is not a duplicate of that pointed Q&A!:

I encountered this problem in relation to Objective-C minor implementation and NSNumber type has varying floating point precision and I just needed to know what really happens and what should I do to resolve this issue. That pointed Q&A doesn't suffice my question with its any answer.

Note: This is not regarding the types usage for the values. I was troubled to understand the behavior of NSNumbers.

I have this very simple method which uses NSNumber basically:

+ (NSNumber*)addNumber:(NSNumber*)firstNumber withSecondNumber:(NSNumber*)secondNumber {
    NSNumber *result = [NSNumber numberWithDouble:([firstNumber doubleValue] + [secondNumber doubleValue])];
    NSLog(@"Result: %@", result);   // This will print out our result for testing
    return result;
}

So if I call this method with parameters as follows:

[Fundamentals addNumber:[NSNumber numberWithDouble:5.0]
       withSecondNumber:[NSNumber numberWithDouble:3.5]];

Printed output: 8.5 ---> Exactly what I was expecting

But if I call this method with the following parameters:

[Fundamentals addNumber:[NSNumber numberWithDouble:50.0]
       withSecondNumber:[NSNumber numberWithDouble:36.54]];

It prints out the following: 86.53999999999999 ---> Which troubled me and still amazed me and questions my brain till it hurts! :(

Note: Not only when printing, even in debugger also it shows the value representation as it is printed out above.

Can somebody explain this please?

Thanks in advanced!

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
  • 1
    http://floating-point-gui.de/ – jtbandes Jul 05 '16 at 05:35
  • Have you tried to use [NSString stringWithFormat:@"%.1f", YourDoubleNumber] ? Should solve your precision – OhadM Jul 05 '16 at 05:49
  • Nope! Actually I don't want to format the output as a string. I need the actual value representation to be a natural one! – Randika Vishman Jul 05 '16 at 05:55
  • Thanks for down voting my question! But can somebody give an answer and why this should be down voted? – Randika Vishman Jul 05 '16 at 06:27
  • This is a duplicate, no matter if it uses NSNumber instead of double. – Rudy Velthuis Jul 05 '16 at 07:00
  • 36.54 cannot be stored exactly in a `double`. `NSNumber` may be the interface of it, but internally, if you tell it to use `double` (`numberWithDouble:`), it uses `double` to store the value, and so what you want cannot be done. This is in fact the same old question once again. – Rudy Velthuis Jul 05 '16 at 07:42

1 Answers1

0

To format float number, you can use NSNumberFormatter

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:22.368511]];

NSLog(@"Result...%@",numberString);//Result 22.37
Proton
  • 1,335
  • 1
  • 10
  • 16