简体   繁体   中英

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

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM