0

I want to round a number (6.756765345678765) 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))
MIkhail
  • 49
  • 8

2 Answers2

1

You can use setprecision(10) function of <iomanip>.

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

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

Output:

6.756765346
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
0

Your issue is not your code. Your issue is the cout itself. You have to set its precision to print the double correctly. Here is a previous question related to your issue: How do I print a double value with full precision using cout?

You can try the same code but with: cout.precision(X) where X is the maximum precision you want to display.

S.Murtadha
  • 42
  • 10