• C++中IO设置数字精度问题


    转载自https://blog.csdn.net/jing_xian/article/details/20239381

    /*setprecision函数控制输出流显示浮点数的有效位数
     * 如果和fixed合用的话,控制小数点右面的位数,fixed的意思是从小数点开始计数
    */
    #include<iostream>
    #include<iomanip> //setprecision函数在这个头文件
    using namespace std;
     
    int main()
    {
    cout << setprecision(8) << 10.0/3 << endl;//输出8位有效数字
    cout << setprecision(8) << 1.0/3 << endl;
    cout << fixed << setprecision(8) << 10.0/13 << endl;//输出到小数点位后八位
    cout << fixed << setprecision(8) << 1.0/13 << endl;
    return 0;
    }
    

      

    参考链接:http://www.cplusplus.com/reference/iomanip/

    // setbase example
    #include <iostream>     // std::cout, std::endl
    #include <iomanip>      // std::setbase
    using namespace std;
    int main () {
         //参数只能为 16 10 8 cout << setbase(16) << 110 << endl; cout << setbase(10) << 110 << endl; cout << setbase(8) << 110 << endl; //2为无效参数 cout << setbase(2) << 110 << endl; return 0; }

      

  • 相关阅读:
    第三章 p62 或运算
    p57 字符串的长度
    p53 ASCII码
    整数类型,如同时钟
    重要:原码、反码、补码...
    p42 实验溢出(上溢)
    P40 字节单位:KMGT
    p38 二、八、十六进制的对应关系
    p13 数组元素的地址
    p11 内存中的数据和地址
  • 原文地址:https://www.cnblogs.com/Virtualmate/p/9662322.html
Copyright © 2020-2023  润新知