tags:

views:

68

answers:

5

how do i round to the second decimal point in C++. thanks for your help.

+1  A: 

You can multiply by 100 and then round to an integer. Then put the decimal point after the first 2 digits.

For example:

void round(double x)
{
   double y = 100 * x;
   int rounded = (int)(y + 0.5);
   printf("%lf rounded = %d.%02d\n", x, rounded / 100, rounded % 100);
}
DasBoot
Note that this will still suffer from IEEE 754's accuracy problems, which is why such things are usually left until output.
Ignacio Vazquez-Abrams
No, this bit won't. The edge case for rounding is 0.5, which is perfectly representable as a binary fraction. I.e. numbers up to `xxxx.01111111111111111` are rounded down to `xxxx`, and numbers from `xxxx.10000000000000000` up are rounded up to `xxxx+1`. (if you _already_ suffer from IEEE754 accuracy problems, it can be surprising though. For instance, if you think that `y` contains `0.145` - it can't.)
MSalters
A: 

Check out http://stackoverflow.com/questions/485525/round-for-float-in-c which talks about rounding floats although not to 2 places. The same basic techniques should work.

Steve Rowe
+1  A: 

If you're expecting an exact result in a double or float, it may be impossible. Many numbers that can be exactly represented in two decimal digits can't be represented in the base 2 floating point numbers at all, and all you'll get is the nearest equivalent. For example you might find that 1.10 is stuck at 1.1000000000000001 no matter how many times you try to round it.

Mark Ransom
A: 

You didn't specify which kind of rounding you need. Assuming rounding to the nearest integer:

#include <math.h>
#include <stdio.h>

double round(double x) { return floor(x * 100 + 0.5) / 100; }

int main()
{
    printf("%g\n", round(12.345));
}

It prints 12.35.

Or if you just want to print a number rounded to two digits after decimal point:

printf("%.2f\n", x);
vitaut
You didn't try your code with 0.145, did you?
Roland Illig
Actually the OP haven't specify the tie breaking rules if you mean that.
vitaut
@Roland Illig: probably not, since IEEE754 cannot represent it. `0.14999999999` should of course be rounded to "0.14"
MSalters
@Roland Illig: As MSalters correctly pointed out 0.145 should be rounded to 0.14 which the code does. So what is your point?
vitaut
Probably I didn't think enough about the code. I suspected that in some cases the expression `x * 100 + 0.5` might result in an inexact/unexpected result that might be avoided. But sure, if you are using `double` for representing decimal numbers you should expect such surprises.
Roland Illig
+1  A: 

When printing doubles you can specify the precision:

f,F

The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.

Try:

printf("%f rounded = %.2f\n", x, x);

The same thing in C++

std::cout << x << " rounded = " << std::setprecision(2) << x << "\n";
Martin York
Curios why somebody thinks this is wrong?
Martin York
Thecnically, this doesn't round `x`, even though it outputs a rounded representation of `x` (not my downvote btw)
MSalters
My argument there is that the question did not specify what he wants rounded (the variable representation or the output of the value). Trying to round a floating point value is doomed to failure (because of rounding issues). Another interpretation is that the OP wants to have a decimal value.
Martin York