C++ 在c+中小数点后显示两位数字+;

C++ 在c+中小数点后显示两位数字+;,c++,precision,C++,Precision,类似的话题已经在论坛上讨论过了。但我在以下代码中遇到了一些不同的问题: double total; cin >> total; cout << fixed << setprecision(2) << total; 双倍合计; cin>>总数; cout使用setiosflags(ios::showpoint)可以实现这一点 将打印1.2 fixed表示小数点后将有固定数量的小数位数 cout << setprecision (2) &

类似的话题已经在论坛上讨论过了。但我在以下代码中遇到了一些不同的问题:

double total;
cin >> total;
cout << fixed << setprecision(2) << total;
双倍合计;
cin>>总数;

cout使用setiosflags(ios::showpoint)可以实现这一点

将打印1.2

fixed
表示小数点后将有固定数量的小数位数

cout << setprecision (2) << fixed << 1.2;

最简单的方法是使用cstdio的printf。事实上,我很惊讶有人提到printf!无论如何,你需要包括图书馆,就像这样

#include<cstdio>

int main() {
    double total;
    cin>>total;
    printf("%.2f\n", total);
}

你试图运行的代码将不会用这个编译器选项运行…

< P>可以用以下方式在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;
#包括
#包括

cout使用头文件
stdio.h
您可以像c一样轻松地完成它。在使用%.2lf(在%说明符之后设置一个特定数字)之前,请使用printf()

它只是在小数点后打印特定的数字

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
   double total=100;
   printf("%.2lf",total);//this prints 100.00 like as C
}
#包括
#包括
使用名称空间std;
int main()
{
双倍总数=100;
printf(“%.2lf”,总计);//这将像C一样打印100.00
}

真的吗?它在我的系统上给出了100.00(好吧,它给出了一个缺少分号的错误,但在我修复后,它按照我的预期工作)。也许你应该发布你测试过的实际代码。你是在要求修复你的代码吗?或者你想用什么方法打印两位小数的数字?您应该发布用于运行代码的编译器选项(或者至少是编译器),这对于发现问题非常有用;)<代码>显示点
在此处不必要,因为他要求小数点后有2位数字。他的代码是正确的,应该给出他想要的输出。
setprecision
的含义取决于浮点输出的格式:
fixed
scientific
floatfmt()
(默认值)。此系统/实现是否依赖?许多人报告说,showpoint不应该是必要的。。。JAMESSKANZE被添加到C++中。这可能是有用的阅读:PrtTf仍然可以在C++中使用,但是在特殊情况下,LOL,UVA判断,LLCIPT或其他任何编译器选项如何与问题都是远程相关的?UHH,3年前,我自己的答案XD,好吧,当我发现这个问题时,我刚开始竞争性编程,所以我认为我的答案中的“额外信息”也可以帮助那些刚开始竞争性编程的人:)
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cpp
#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;
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
   double total=100;
   printf("%.2lf",total);//this prints 100.00 like as C
}