C++ 在C++中显示小数点后两位数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16280069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show two digits after decimal point in c++
提问by DSKVP
Similar topic is already discussed in the forum. But I have some different problem in following code:
论坛中已经讨论过类似的话题。但是我在以下代码中有一些不同的问题:
double total;
cin >> total;
cout << fixed << setprecision(2) << total;
If I give input as 100.00
then program prints just 100
but not 100.00
如果我给出输入,100.00
那么程序只打印100
但不打印100.00
How can I print 100.00
?
如何打印100.00
?
回答by Armen Tsirunyan
cout << fixed << setprecision(2) << total;
setprecision
specifies the minimumprecision. So
setprecision
指定最小精度。所以
cout << setprecision (2) << 1.2;
will print 1.2
将打印 1.2
fixed
says that there will be a fixed number of decimal digits after the decimal point
fixed
表示小数点后会有固定的小数位数
cout << setprecision (2) << fixed << 1.2;
will print 1.20
将打印 1.20
回答by soehtetZ
It is possible to print a 15 decimal number in C++ using the following:
可以使用以下命令在 C++ 中打印 15 位十进制数:
#include <iomanip>
#include <iostream>
cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();
return 0;
回答by Bengalaa
The easiest way to do this, is using cstdio's printf. Actually, i'm surprised that anyone mentioned printf! anyway, you need to include the library, like this...
最简单的方法是使用 cstdio 的 printf。实际上,我很惊讶有人提到 printf!无论如何,你需要包括图书馆,像这样......
#include<cstdio>
int main() {
double total;
cin>>total;
printf("%.2f\n", total);
}
This will print the value of "total" (that's what %
, and then ,total
does)with 2 floating points (that's what .2f
does). And the \n
at the end, is just the end of line, and this works with UVa's judge online compiler options, that is:
这将打印“total”的值(这就是%
,然后,total
是)2 个浮点(这就是这样.2f
做的)。而\n
在最后,是行刚刚结束,而这个作品经UVA的法官在线编译器选项,那就是:
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cpp
the code you are trying to run will not run with this compiler options...
您尝试运行的代码将无法使用此编译器选项运行...
回答by woodleg.as
This will be possible with setiosflags(ios::showpoint).
这可以通过 setiosflags(ios::showpoint) 实现。
回答by Sofi Ullah Saikat
Using header file stdio.h
you can easily do it as usual like c. before using %.2lf(set a specific number after % specifier.) using printf().
使用头文件,stdio.h
您可以像往常一样轻松地完成它,就像 c. 在使用 %.2lf(在 % 说明符之后设置一个特定数字。)之前使用 printf()。
It simply printf specific digits after decimal point.
它只是在小数点后打印特定的数字。
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
double total=100;
printf("%.2lf",total);//this prints 100.00 like as C
}