21

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

mskfisher
  • 3,291
  • 4
  • 35
  • 48
ghiboz
  • 7,863
  • 21
  • 85
  • 131
  • 2
    Here you go! http://stackoverflow.com/questions/829067/how-can-i-round-a-float-value-to-2-post-decimal-positions – EmptyStack Jul 13 '11 at 11:23
  • possible duplicate of [Rounding numbers in Objective-C](http://stackoverflow.com/questions/752817/rounding-numbers-in-objective-c) – jscs Jul 13 '11 at 18:37

2 Answers2

59

I don't know 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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nathan Day
  • 5,981
  • 2
  • 24
  • 40
  • 1
    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 06:43
  • 1
    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 02: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
  • @adamek: You should not round to the nearest cent, you should find out if there are any legal rules (legal as in tax law, trading laws etc. ) that tell you how to round, and round accordingly, if you are calculating numbers that are not purely informational. – gnasher729 Apr 14 '15 at 10:00
  • That's why I use RoundBankers mode. . enum NSRoundingMode : UInt { case RoundPlain case RoundDown case RoundUp case RoundBankers } – adamek Apr 14 '15 at 14:17
30

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.

Stunner
  • 12,025
  • 12
  • 86
  • 145
gaige
  • 17,263
  • 6
  • 57
  • 68
  • 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. – al45tair 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