如何在C ++中使用setprecision [英] How to use setprecision in C++

查看:195
本文介绍了如何在C ++中使用setprecision的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C ++ 的新手,我只想输出最多2位的点号。
就像数字是 3.444 一样,则输出应该是 3.44 或数字是 99999.4234 ,则输出应为 99999.42 ,我该怎么做。该值是动态的。这是我的代码。

I am new in C++ , i just want to output my point number up to 2 digits. just like if number is 3.444, then the output should be 3.44 or if number is 99999.4234 then output should be 99999.42, How can i do that. the value is dynamic. Here is my code.

#include <iomanip.h>
#include <iomanip>
int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}

但它给了我一个错误,未定义的固定符号。

but its giving me an error, undefined fixed symbol.

推荐答案

#include <iomanip>
#include <iostream>

int main()
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2);
    std::cout << num1 << std::endl;
    return 0;
}

这篇关于如何在C ++中使用setprecision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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