iOS Objective-C How to get 2 decimal rounded float value?

57,120

Solution 1

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.

Solution 2

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.

Solution 3

Error in this Line

a = [[NSString stringWithFormat:@"%.2f", a] floatValue];

correct it to

a = [NSString stringWithFormat:@"%.02f", a];

Solution 4

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:

  • You can use use formatting that displays “1412.24”, without changing the underlying float object.
  • You can use double to get closer (much closer), but it will still not be exactly 1412.24.
  • You can scale float values to representable values (e.g., measure in other units so that the values you want are all exactly representable in float).
  • You can use a different data type, such as NSDecimal.

Solution 5

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.

Share:
57,120
B-Man
Author by

B-Man

I am an aspiring iOS developper. 3 Apps in the appstore so far. Need help where my knowledge is lacking and want to offer help when my knowledge is flowing. ♥ I DIG PEOPLE THAT: make others laugh, have a burning passion in life, take big risks, smile a lot, can explain complicated things in interesting ways, play music, bring out the best in others, buy food for homeless people, feel a lot, act in the face of fear, enjoy talking about others more than themselves, make others laugh and dont repeat themselves.

Updated on July 31, 2020

Comments

  • B-Man
    B-Man almost 4 years

    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).