C ++流输出,小数点后有3位数字。怎么样? [英] C++ stream output with 3 digits after the decimal point. How?

查看:623
本文介绍了C ++流输出,小数点后有3位数字。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有浮点型变量,并希望得到小数点后3位数的输出,使用c ++中的iostream。如何才能做到这一点?

解决方案

使用 setf precision

  #include< iostream> 

使用namespace std;

int main(){
double f = 3.14159;
cout.setf(ios :: fixed,ios :: floatfield);
cout.precision(3);
cout<< f< ENDL;
返回0;



$ b $打印 3.142

If i have float type variable and want to get output with 3 digits after the decimal point, using iostream in c++. How I can do it?

解决方案

Use setf and precision.

#include <iostream>

using namespace std;

int main () {
    double f = 3.14159;
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(3);
    cout << f << endl;
    return 0;
}

This prints 3.142

这篇关于C ++流输出,小数点后有3位数字。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆