当前位置:首页C++ > 正文

c++ fixed

作者:野牛程序员:2025-11-10 18:54:55C++阅读 2263
c++ fixed
/*
C++ fixed
作用:让浮点数以定点格式输出,不再使用科学计数法
头文件:<iomanip>
与 setprecision 一起使用可控制小数位数
*/
#include <iostream>
#include <iomanip>   // fixed、setprecision
using namespace std;
int main() {
    double v = 123.456789;
    // 默认输出
    cout << v << endl;
    // fixed:定点输出
    cout << fixed << v << endl;
    // fixed + setprecision:保留小数位
    cout << fixed << setprecision(3) << v << endl; // 123.457
    // 恢复正常输出(取消 fixed)
    cout.unsetf(ios::fixed);
    cout << v << endl;
    return 0;
}
/*
要点:
1) fixed:定点格式,不使用科学计数法
2) setprecision(n):控制小数位数
3) 取消 fixed:cout.unsetf(ios::fixed)
*/


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
  • c++ fixed
  • 相关推荐

    最新推荐

    热门点击