This question already has an answer here:
这个问题在这里已有答案:
- Printing the correct number of decimal points with cout 11 answers
使用cout 11答案打印正确的小数点数
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?
5 个解决方案
#1
51
cout << fixed << setprecision(2) << total;
setprecision
specifies the minimum precision. 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
#2
2
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”的值(即%,然后,总值),带有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...
您尝试运行的代码将无法使用此编译器选项运行...
#3
2
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;
#4
1
This will be possible with setiosflags(ios::showpoint).
setiosflags(ios :: showpoint)可以实现这一点。
#5
1
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,您可以像往常一样轻松地执行此操作。在使用%。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
}
#1
51
cout << fixed << setprecision(2) << total;
setprecision
specifies the minimum precision. 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
#2
2
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”的值(即%,然后,总值),带有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...
您尝试运行的代码将无法使用此编译器选项运行...
#3
2
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;
#4
1
This will be possible with setiosflags(ios::showpoint).
setiosflags(ios :: showpoint)可以实现这一点。
#5
1
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,您可以像往常一样轻松地执行此操作。在使用%。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
}