Reputation: 2249
I have a
float a = 1412.244019;
and I need a to be rounded to the nearest second decimal like 1412.24000000. I understand that if i want to present a with only two decimals i use %.2f, but that is NOT the case. I need the new a for mathematical reasons as a float.
I have tried a couple of methods found on stackoverflow without luck. The more obvious one i used, i mention below, but still had no luck using it. Can YOU see why the magic is not happening?
PS: It does do the desired effect sometimes, NOT always, which gets me thinking... hmmm...
Thanks in advance.
Code:
float a = 1412.244019;
NSLog(@"a is %f", a); //output a is 1412.244019
a = [[NSString stringWithFormat:@"%.2f", a] floatValue];
NSLog(@"a is %f", a); //output a is 1412.239990
EDIT:
SEEMS like when i am using the float a after the above surgery, it is considering it to be 1412.240000 eventhough the NSLog says differently... strange. But i am getting what I want, so Kudos for wasted time chasing nothing :)
EDIT I would love to choose you all as correct answers, but since i can only choose one, i chose the first good answer with extra explanation (of the two last).
Upvotes: 29
Views: 58092
Reputation: 123
Error in this Line
a = [[NSString stringWithFormat:@"%.2f", a] floatValue];
correct it to
a = [NSString stringWithFormat:@"%.02f", a];
Upvotes: 6
Reputation: 52622
You do that in Objective-C exactly the same as you would in C:
double notRounded = ...;
double rounded = round (notRounded * 100.0) / 100.0;
Don't use float - unless you can explain to someone exactly what your specific reasons are to use float over double. float has very limited precision. And using ceil or floor is daft, since there is a perfectly fine standard function named "round".
Neither float nor double can exactly represent most decimal values, like 12.24. Because it has much higher precision, double will let you get much closer to 12.24. Therefore, there is a huge chance that NSLog will display some weird number for (float) 12.24, like 12.23999997394 or whatever, while it may display 12.24 for double.
Upvotes: 11
Reputation: 11675
Have you tried this?
CGFloat val = 37.777779;
CGFloat rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
CGFloat nearest = floorf(val * 100 + 0.5) / 100; /* Result: 37.78 */
CGFloat rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
source : Rounding Number to 2 Decimal Places in C
Complementing: You just don't have control about how the computer will store the float value.
So these rounded values may not be EXACTLY the "obvious" decimal values, but they will be very very close, and that's the max guarantee you will have.
Upvotes: 60
Reputation: 224102
The closest value to 1412.24 that a float
can have is 1412.239990234375. There is no operation that can produce a float
any closer than that, because the format of float
objects does not represent any closer values. Asking to represent 1412.24 in float
is like asking to represent 2.5 in int
. It simply cannot be done.
Options for dealing with this include:
float
object.double
to get closer (much closer), but it will still not be exactly 1412.24.float
values to representable values (e.g., measure in other units so that the values you want are all exactly representable in float
).NSDecimal
.Upvotes: 5
Reputation: 5935
You can't force a float value. Floats are designed to be somewhat approximate, as their magnitude can be great, and since you've only got so many bits to use, it can get close to, but not exactly express a decimal number.
One suggestion is to do all your arithmetic in integer, multiplied by 100, then dividing when you want to display your final result. You may have numbers too large which would prohibit this.
Upvotes: 2
Reputation: 1399
You could multiply a by 100.0 and perhaps add 0.5 for rounding and then take the int value of the result. You can then use / 100 to get the value before the decimal point and % 100 to get the two decimal places.
Upvotes: 2