how to round a number to N decimal places in C++?

Question:

I want to round a number to 10 decimal places , but it returns ‘6.75677’, although it could returns ‘6.7567653457’

#include <iostream>
#include <cmath>
using namespace std;

double rounded(double number, int N)
{
    return round(number * pow(10, N)) / pow(10, N); // i've chanched float to double
}

int main()
{
    double value = 6.756765345678765; 
    cout << rounded(value, 10)
}

I’d like to see a function returns rounded number

Frankly speaking, I’d see an alternative of function ’round’ in python

print(round(6.756765345678765, 10))
Asked By: MIkhail

||

Answers:

cout << fixed << setprecision(10) << value << endl
Answered By: marcin-dolowy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.