I am wondering if there is a way that you can easily and safely correct floating point numbers.
For example,
When entered: " 32 + 32.1 " Result: "64.0999999999999"
Also I must mention that this occurs quite frequently when using scientific notation. " ( 2.3 * 10^23)*(1.452 * 10^23) " Returns: " 3.339599999999999999e+46"
And finally, sometimes the number that is returned is: ex. 123.0000000000001
Thanks for the help!
EDIT
The answer that was approved is great. But what I found worked for me was using %g with a double in NSString stringWithFormat. %g seems to round everything quite appropriately. ex.
answer.text = [NSString stringWithFormat@" %g ", doubleAnswer];
Using doubles through your calculations and then using that method seemed to work for me, and I hope this helps others as well. If this isn't the answer your looking for, check out the approved answer!
See Question&Answers more detail:os