Can someone please show me the way to set the precision of a float number to desired length. Say I have a number 2504.6. As you see the precision here is only 1. I want to set it to six.I need this because I compare this value with the value obtained from [txtInput.text floatValue]. And even if I enter 2504.6 to the text box it will add 5 more precisions and will be 2504.600098. And when I compare these two values they appear to be not equal.
-
Have you tried `double`? – Oliver Charlesworth Feb 26 '12 at 17:50
-
Not I have not, would it matter? I mean, isn't double just twice bigger than float? I mean the precisions. – Mikayil Abdullayev Feb 26 '12 at 19:15
-
A `double` gives you approximately twice as much precision. – Oliver Charlesworth Feb 26 '12 at 19:18
3 Answers
Floats are approximates. The way floats are stored does not allow for arbitrary precision. Floats (and doubles) are designed to store very large (or small) values, but not precise values.
If you need a very precise non-integer number, use an int (or long) and scale it. You could even write your own object class to handle that.
They won't appear to be equal
Btw this question has been asked before
Comparing float and double data types in objective C
-
1A long only gives you about 3 decimal digits more precision than a double. – Oliver Charlesworth Feb 26 '12 at 17:55
-
-
@Sulthan: Define "approximation". A float **is** an integer with an associated scaling factor. – Oliver Charlesworth Feb 26 '12 at 18:05
-
Float cannot represent all the values. A 2.4 put into a scaled long will always stay 2.4. A 2.4 put into a float is immediately converted into a 2.4000000001 or 2.3999999999 – Sulthan Feb 26 '12 at 18:33
-
@Sulthan: A scaled long cannot represent "all the values" either. – Oliver Charlesworth Feb 26 '12 at 18:45
-
Comparing two float variables A and B using 'equal' operator is not very good idea, cause float numbers have limited precision. The best way to compare floats is
fabs(A - B) < eps
where eps is some very small value, say 0.0001
If you're operating with strings that represent the float values you can just compare strings and not the numbers.

- 16,679
- 4
- 44
- 57
-
This only works if OP doesn't need the comparison result to be *correct* (I mean without approximations) if the user actually inputs 2504.600098. – sch Feb 26 '12 at 18:08
You can compare the numbers using NSDecimalNumber
:
NSDecimalNumber *number = [NSDecimalNumber numberWithFloat:2504.6f];
NSDecimalNumber *input = [NSDecimalNumber decimalNumberWithString:txtInput.text];
NSComparisonResult result = [number compare:input];
if (result == NSOrderedAscending) {
// number < input
} else if (result == NSOrderedDescending) {
// number > input
} else {
// number == input
}

- 27,436
- 3
- 68
- 83