Reputation: 12367
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.
Upvotes: 1
Views: 4053
Reputation: 27506
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
}
Upvotes: 3
Reputation: 16719
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.
Upvotes: 3
Reputation: 2320
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
Objective-C Float / Double precision
Make a float only show two decimal places
Upvotes: 4