Reputation: 1
Can any one please help me how to get float value as it is from text box
for Ex: I have entered 40.7
rateField=[[rateField text] floatValue];
I am getting rateField
value as 40.7000008
but I want 40.7
only.
please help me.
thanks in advance
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks®ards Balu
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks®ards Balu
Upvotes: 0
Views: 887
Reputation: 6084
You are already resolved float value.
Floating point numbers have limited precision. Although it depends on the system, float relative error due to rounding will be around 1.1e-8 Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
So if you're using those numbers for output, you should use some rounding mechanism, even for double values.
Upvotes: 0
Reputation: 31
When using floating point numbers, these things can happen because of the way the numbers are stored in binary format in the computers memory.
It's similar to the way 1/3 = 0.33333333333333... in decimal numbers.
The best way to deal with this is to use number formatters in the textbox that displays the value.
Upvotes: 0
Reputation: 17877
This is ok. There is not guaranteed that you will get 40.7
if you will use even double
.
If you want to output 40.7
you can use %.1f
or NSNumberFormatter
Upvotes: 3
Reputation: 19267
Try using a double
instead. Usually solves that issue. Has to do with the storage precision.
double dbl = [rateField.text doubleValue];
Upvotes: 0