如何使用cin来读取double的全部值? [英] How to read the entire value of a double using cin?

查看:309
本文介绍了如何使用cin来读取double的全部值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输入:

输入double:1.546640625

enter double: 1.546640625

输出:

m = 1.54664

m = 1.54664

我必须转换成一个二进制的点,当我读取数字,如2.359375000

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

输出:

= 2.35938

m = 2.35938

它的工作原理,但我认为问题是零点1.546640625

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

推荐答案

你已经读取了double的全部值。问题是cout。默认情况下将小数点后的值舍入到6位数。

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.

要设置精度cout使用,请使用 setprecision < iomanip>

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

这篇关于如何使用cin来读取double的全部值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆