如何用C ++流输出小数点后3位? [英] How to output with 3 digits after the decimal point with C++ stream?

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

问题描述

给出一个float类型的变量,如何在C ++中使用iostream将其输出到小数点后3位?

Given a variable of float type, how to output it with 3 digits after the decimal point, using iostream in C++?

推荐答案

使用 setf 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;
}

此打印 3.142

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

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