zoukankan      html  css  js  c++  java
  • c++中cout控制输出精度

    #include <iostream>
    #include <iomanip> //需要包含的头文件
    using namespace std;

    int main()
    {
    //可以直接在开头设置往下小数精度 cout << fixed << setprecision(2) 往下小数全为2位精度
    double value = 12.34567890;
    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; a// fixed和setprecision的作用还在,依然显示12.3457
    cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
    cout << value << endl;
    cout.precision( 6 ); // 恢复初始状态,输出为12.3457
    cout << value << endl;
    }
    #END
  • 相关阅读:
    [LeetCode] Wiggle Sort
    [LeetCode] Perfect Squares
    [LeetCode] Minimum Window Substring
    [LeetCode] Valid Sudoku
    [LeetCode] Sudoku Solver
    [LeetCode] First Bad Version
    [LeetCode] Find the Celebrity
    [LeetCode] Paint Fence
    [LeetCode] H-Index II
    [LeetCode] H-Index
  • 原文地址:https://www.cnblogs.com/yunet/p/12584104.html
Copyright © 2011-2022 走看看