C++ 使用cout打印正确的小数点数
我有一个C++ 使用cout打印正确的小数点数,c++,C++,我有一个float值的列表,我想用cout和两位小数打印它们 例如: 10.900 should be printed as 10.90 1.000 should be printed as 1.00 122.345 should be printed as 122.34 我该怎么做 (setprecision在这方面似乎没有帮助。)使用,您可以使用和 这里有一个例子 #include <iostream> #include <iomanip> int main()
float
值的列表,我想用cout
和两位小数打印它们
例如:
10.900 should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34
我该怎么做
(setprecision
在这方面似乎没有帮助。)使用
,您可以使用和
这里有一个例子
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
setprecision(n)
适用于整数,而不是小数部分。您需要使用定点格式使其应用于小数部分:setiosglags(ios::fixed)
您就快到了,还需要使用std::fixed,请参阅
您必须将“浮动模式”设置为“固定”
float num = 15.839;
// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
float num=15.839;
//这将输出15.84
std::cout简化已接受的答案
简化示例:
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}
参考:
这是一个使用矩阵的示例
cout<<setprecision(4)<<fixed<<m[i][j]
cout只是一个小问题;将以下内容放在标题中
使用名称空间std
然后
std::cout要在小数点后设置固定的2位数字,请首先使用这些数字:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
然后打印您的双值
这是一个例子:
#include <iostream>
using std::cout;
using std::ios;
using std::endl;
int main(int argc, char *argv[]) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double d = 10.90;
cout << d << endl;
return 0;
}
#包括
使用std::cout;
使用std::ios;
使用std::endl;
int main(int argc,char*argv[]){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
计算精度(2);
双d=10.90;
不能包含
int main()
{
双d=15.64545347;
printf(“%0.2lf”,d);
}
我在一次编码比赛中遇到了类似的问题,我就是这样处理的。
将精度2设置为所有双精度值
首先添加标题以使用setprecision
#包括
然后在我们的主程序中添加以下代码
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
您需要使用fixed来编写5.00,这就是为什么5.00时您的输出不会出现
我在需要一致格式时遇到了整数问题
完整性重写:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// floating point formatting example
cout << fixed << setprecision(2) << 122.345 << endl;
// Output: 122.34
// integer formatting example
cout << fixed << setprecision(2) << double(122) << endl;
// Output: 122.00
}
#包括
#包括
使用名称空间std;
int main()
{
//浮点格式示例
不能使用模板
#include <iostream>
// d = decimal places
template<int d>
std::ostream& fixed(std::ostream& os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
int main(){
double d = 122.345;
std::cout << fixed<2> << d;
}
#包括
//d=小数位
模板
标准::ostream&fixed(标准::ostream&os){
setf(std::ios_base::fixed,std::ios_base::floatfield);
os.精度(d);
返回操作系统;
}
int main(){
双d=122.345;
std::cout#包括
#包括
#包括
使用名称空间std;
int main(){
INTA;
长int b;
字符c;
浮动d;
双e;
cin>>a>>b>>c>>d>>e;
你为什么在程序中使用“std:fixed”呢?可以为此定义一个有用的标题:#define fixed_FLOAT(x)std::fixed@VilasJoshi,setprecision设置小数点后的位数,如果有5位,我们使用setprecision(2)我们将得到2个数字,但如果有0个数字,它将显示无,使用fixed,我们修复了需要显示的数字,因此5将表示为5.00无5@vaibnak这一点是错误的。setprecision
设置了“位数”,这取决于std::fixed
设置的状态“有效数字”、“小数点后的数字”或“十六进制后的数字”感谢平头日历。其他人注意“小数点后的n位”确实在fixed中使用了setprecision这对我来说很有效:std::cout由于您没有使用名称空间,所以在cout和endl之前缺少std::谢谢。添加了头并使用了名称空间std。是的,它确实变得“简化”,但这是非常不鼓励的。请不要为了它而使用使用名称空间std;
-理解您为什么这样做。
#include <iostream>
using std::cout;
using std::ios;
using std::endl;
int main(int argc, char *argv[]) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double d = 10.90;
cout << d << endl;
return 0;
}
#include<stdio.h>
int main()
{
double d=15.6464545347;
printf("%0.2lf",d);
}
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
5.99
5.00
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// floating point formatting example
cout << fixed << setprecision(2) << 122.345 << endl;
// Output: 122.34
// integer formatting example
cout << fixed << setprecision(2) << double(122) << endl;
// Output: 122.00
}
#include <iostream>
// d = decimal places
template<int d>
std::ostream& fixed(std::ostream& os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
int main(){
double d = 122.345;
std::cout << fixed<2> << d;
}
// d = decimal places
template<int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
// d = decimal places
template<int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
int main(){
double d = 122.345;
std::cout << f<10,2> << d << '\n'
<< e<10,2> << d << '\n';
}
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
int main() {
int a;
long int b;
char c;
float d;
double e;
cin>>a>>b>>c>>d>>e;
cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
cout<<fixed<<setprecision(3)<<d<<"\n";
cout<<fixed<<setprecision(9)<<e;
return 0;
}