Home c++ Rounding to N decimal places in C++

Rounding to N decimal places in C++

Author

Date

Category

is a number of type double , for example 4.64452675

How to round to N decimal places in C++ ? Or clipping? Didn’t find the function I needed


Answer 1, authority 100%

static const double powerOfTen [] = {1.0, 10.0, 100.0, ...};
double truncated = std :: trunc (d * powerOfTen [N]) / powerOfTen [N];
double rounded = std :: floor (d * powerOfTen [N] + 0.5) / powerOfTen [N];

Answer 2, authority 90%

# include & lt; iostream & gt;
#include & lt; cstdlib & gt;
#include & lt; cmath & gt; // for round
using namespace std;
int main (int argc, char ** argv)
{
 double value = 4.64459675;
 // Positive numbers
 cout & lt; & lt; round (value * 10) / 10 & lt; & lt; endl; // Round to the first character
 cout & lt; & lt; round (value * 100) / 100 & lt; & lt; endl; // second
 cout & lt; & lt; round (value * 1000) / 1000 & lt; & lt; endl; // third
 cout & lt; & lt; round (value * 10000) / 10000 & lt; & lt; endl; // fourth
 // Negative numbers
 cout & lt; & lt; round (-value * 10) / 10 & lt; & lt; endl; // Round to the first character
 cout & lt; & lt; round (-value * 100) / 100 & lt; & lt; endl; // second
 cout & lt; & lt; round (-value * 1000) / 1000 & lt; & lt; endl; // third
 cout & lt; & lt; round (-value * 10000) / 10000 & lt; & lt; endl; // fourth
 return EXIT_SUCCESS;
}

Result:

4.6
4.64
4.645
4.6446
-4.6
-4.64
-4.645
-4.6446

Output

The number of zeros is equal to the number of decimal places


Answer 3, authority 29%

Streams (like std :: wstringstream ) have this:


std :: setw (4):
wstream & lt; & lt; std :: setw (4) // set 4 characters

AND

std :: hex:
wstream & lt; & lt; std :: hex // setting a hexadecimal inscription (there is also a scientific one and I don't remember which one)

Answer 4, authority 29%

I think it’s easier:

std :: stringstream stream;
stream & lt; & lt; std :: fixed & lt; & lt; std :: setprecision (N) & lt; & lt; d;
stream & gt; & gt; d;

Answer 5

# include & lt; iostream & gt;
#include & lt; cmath & gt;
using namespace std;
int main () {
 double value; //number
 doudle denom; // desired precision
 cout & lt; & lt; (value-remainder (value, denom));
 return 0;
}

Answer 6

double _trim_tail (
   const double & amp; p_value
  , const int p_decimal_places = 4
  , const int p_make_round = 1
) {
  double exponent = round (log10 (fabs (p_value)));
  double res = p_value * pow (10, -exponent);
  if (fabs (res) & lt; 1.0) {
    res = res * 10.0;
    exponent - = 1;
  } else if (fabs (res) & gt; = 10.0) {
    res = res / 10.0;
    exponent + = 1;
  }
  if (p_make_round! = 0) {
    res = round (res * pow (10, p_decimal_places)) / pow (10, p_decimal_places);
  } else {
    res = trunc (res * pow (10, p_decimal_places)) / pow (10, p_decimal_places);
  }
  res = res * pow (10, exponent);
  return res;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions