首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我输入的数字只读到6位有效数字?

为什么我输入的数字只读到6位有效数字?
EN

Stack Overflow用户
提问于 2012-09-13 05:28:53
回答 1查看 393关注 0票数 1

我正在尝试运行下面的程序,它可以工作,但当我输入一个超过6位小数的值时,它会一直四舍五入/截断,例如2.999999 --> 3。我如何设置它,使它停止这样做?

代码语言:javascript
复制
int main()
{

    double n=0, x=0; 

    while (cin >> n >> x) //will keep going until an integer is not entered
    {
       cout << "You entered the two integers " << x << " and " << n << endl;

       if (x-n <= (1.0/10000000) && n-x <= (1.0/10000000)) 
          cout << "The numbers are almost equal" << endl;
    }

return 0;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-13 05:40:06

可以使用std::setprecision更改打印值的精度

代码语言:javascript
复制
cout << "You entered the two integers " << setprecision(20) << x
     << " and " << n << endl;

Link to ideone.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12396437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档