双重乘法给出四舍五入的结果

发布于 2024-09-30 23:43:37 字数 156 浏览 1 评论 0原文

double a = 2451550;
double b = .407864;
double c= a*b;
cout<<c;

我期望结果是“999898.9892”,但得到“999899”。我需要实际的未四舍五入的结果。请提出建议。

double a = 2451550;
double b = .407864;
double c= a*b;
cout<<c;

I was expecting the results to be "999898.9892" but getting "999899". I need the actual unrounded result.Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2024-10-07 23:43:37

默认情况下,iostreams 输出 6 位精度。如果你想要更多,你必须提出要求:

std::cout.precision(15);

By default, iostreams output 6 digits of precision. If you want more, you have to ask for it:

std::cout.precision(15);
乖乖兔^ω^ 2024-10-07 23:43:37

也可以使用 Manipulator set precision 来完成,如下所示。

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
 double a = 2451550;
 double b = .407864;
 double c= a*b;
 cout<<setprecision(15)<<c;
}

此外,操纵器的使用将使代码变得紧凑。

It can also be done using Manipulator setprecision like below.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
 double a = 2451550;
 double b = .407864;
 double c= a*b;
 cout<<setprecision(15)<<c;
}

Also, Usage of manipulator will make the code compact.

雨巷深深 2024-10-07 23:43:37

默认情况下,std::iostream 的精度将显示要显示的总共位数,默认精度为 6。因此,由于您的数字小数点前有六位数字地方它不会显示任何小数点后。

您可以使用“固定”操纵器更改此行为。然后“精度”更改为小数点后显示的位数,这可能正是您所期望的。

要始终获得小数点后四位数字,您可以执行以下操作:

cout << setprecision(4) << fixed << c;

但是,请记住,这将始终显示小数点后四位数字,即使它们为零。没有简单的方法可以让 std::iostreams 的“精度”表示至多 x 小数点后的位数。

By default the precision of an std::iostream will show how many digits total to display and by default precision is 6. So, since your number has six digits before the decimal place it will not display any after the decimal.

You can change this behavior with the 'fixed' manipulator. 'precision' then changes to mean the number of digits to display after the decimal which is probably what you were expecting.

To always get four digits after the decimal you can do this:

cout << setprecision(4) << fixed << c;

However, keep in mind that this will always display four digits after the decimal even if they are zeros. There is no simple way to get 'precision' to mean at most x number of digits after the decimal place with std::iostreams.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文