Take the 2-minute tour ×

I have a floating point number that have more decimal digits, for example:

float fRes = 10.0 / 3.0;

actually the fRes value is 3.3333333333333 it's possible set for example 2 decimal digits:

float fRes = 10.0 / 3.0;
// fRes is 3.333333333333333333333333
float fResOk = FuncRound( fRes, 2 );
// fResOk is 3.33

thanks in advance

share|improve this question
Here you go! stackoverflow.com/questions/829067/… –  EmptyStack Jul 13 '11 at 11:23
thanks EmptyStack –  ghiboz Jul 13 '11 at 11:25
Welcome ghiboz! –  EmptyStack Jul 13 '11 at 11:43
possible duplicate of Rounding numbers in Objective-C –  Josh Caswell Jul 13 '11 at 18:37
add comment

2 Answers

up vote 16 down vote accepted

Assuming that you're looking for the correct function to round to a certain number of digits, you'll probably find it easiest to do the following: fResOk = roundf( fRes*100.0)/100.0;

That will multiply the value by 100 (giving you your 2 digits of significance), round the value, and then reduce it back to the magnitude you originally started with.

share|improve this answer
This won't actually work, in general, because floating point uses a binary point, not a decimal point, and so lots of numbers that are exact in decimal are not exactly representable when using floating point arithmetic. –  alastair Jul 13 '11 at 15:33
True enough, if you are looking for an exact value. However, considering that this is a rounding problem in the first place, the presumption was that a number that approximated within the accuracy of the machine's internal representation would be sufficient. If you really need a number that is rounded to exactly two decimal points, you should either create a fixed point number system (none built in to iOS or Objective-C) or you can multiply by 100 in the beginning and coerce to an integer, and then divide by 100 after you have used the ration later (basically simulating your own fixed). –  gaige Jul 13 '11 at 20:50
add comment

I don't now where you are using this rounded number, but you should only round your value when displaying it to the user, there are C based format string ways to round floating point numbers for example

[NSString stringWithFormat:@"%.2f", value];

as you may have already read, floating point number are approximations of real numbers, so doing fResOk = roundf( fRes*100.0)/100.0; may not give you 3.33 but a number which is just as close as you can get with floating point number to 3.33.

share|improve this answer
On one hand, sometimes you should round your number. When working with dollars you should normally round to the nearest cent. On the other hand, if you are working with enough money that rounding in your calculations is that important, then you should use decimal math (NSDecimal). –  adamek Oct 13 '12 at 6:43
You should't round even then, every time you round you are introducing an error, you are not adding any better information to you number you are taking away information, this includes when outputting to a string. For currencies I would use integers representing cents. –  Nathan Day Oct 16 '12 at 2:44
NSDecimal provides better accuracy for some calculations compared to using integers for cents. You can define the rounding behavior instead of using the truncating that often happens with integer division. Think about sales tax calculations. On the other hand, decimal math calculations are significantly slower than either integer or float calculations. –  adamek Oct 24 '12 at 14:22
add comment

Your Answer


