1

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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206

3 Answers3

4

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

Community
  • 1
  • 1
rfcdejong
  • 2,219
  • 1
  • 25
  • 51
3

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.

Max
  • 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
3

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
}
sch
  • 27,436
  • 3
  • 68
  • 83