0
点赞
收藏
分享

微信扫一扫

【C++】指定小数输出位数

舍予兄 2022-04-27 阅读 47
c++

引入头文件iomanip
指定输出位数主要由setprecision()和fixed()负责
加上fixed意味着是固定点方式显示,精度指的是小数位
unsetf( ios::fixed ); 去掉fixed,精度恢复成整个数值的有效位数

#include <iostream>[添加链接描述](https://blog.csdn.net/xiongyangg/article/details/24439295)
#include <iomanip>
using namespace std;
 
int main( void )
{
    const double value = 12.3456789;
 
    cout << value << endl; // 默认以6精度,所以输出为 12.3457
    cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
    cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
    cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
    cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
    cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
    cout << value << endl;
    cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
    cout << value << endl;
}

参考资料:
C++中指定小数位数输出

举报

相关推荐

0 条评论