Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the entire value of a double using cin?

Tags:

c++

double

long double m;
cout << "enter double: "; cin >> m;
cout << "m = " << m <<endl;

Input:

enter double: 1.546640625

Output:

m = 1.54664

I have to convert into a binary with point, and when I read numbers like 2.359375000

Output:

m = 2.35938

And it works, but I think the problem is the zero in 1.546640625

like image 236
freinn Avatar asked Oct 28 '12 09:10

freinn


1 Answers

You have read the whole value of the double. The problem is with the cout. It by default rounds the value to 6 digits after the decimal point.

To set the precision cout uses, use setprecision from <iomanip>:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    long double d;
    cin >> d;
    cout << setprecision(10) << d << endl;
    return 0;
}
like image 137
Ivaylo Strandjev Avatar answered Oct 14 '22 20:10

Ivaylo Strandjev