5

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

mike
  • 51
  • 1
  • 1
  • 2

5 Answers5

3

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);
}
Himadri Choudhury
  • 10,217
  • 6
  • 39
  • 47
  • 1
    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 Oct 27 '10 at 05:33
  • 2
    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 Oct 27 '10 at 08:40
3

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";
Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Curios why somebody thinks this is wrong? – Martin York Oct 27 '10 at 07:47
  • 3
    Thecnically, this doesn't round `x`, even though it outputs a rounded representation of `x` (not my downvote btw) – MSalters Oct 27 '10 at 08:41
  • 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 Oct 27 '10 at 09:50
2

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
  • 299,747
  • 42
  • 398
  • 622
1

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
  • 49,672
  • 25
  • 199
  • 336
  • You didn't try your code with 0.145, did you? – Roland Illig Oct 27 '10 at 07:37
  • 1
    Actually the OP haven't specify the tie breaking rules if you mean that. – vitaut Oct 27 '10 at 08:12
  • 1
    @Roland Illig: probably not, since IEEE754 cannot represent it. `0.14999999999` should of course be rounded to "0.14" – MSalters Oct 27 '10 at 08:46
  • @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 Oct 27 '10 at 08:58
  • 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 Oct 27 '10 at 22:22
0

Check out round() for float in C++ which talks about rounding floats although not to 2 places. The same basic techniques should work.

Community
  • 1
  • 1
Steve Rowe
  • 19,411
  • 9
  • 51
  • 82