c++ cout << [double] not printing decimal places

前端 未结 2 475
盖世英雄少女心
盖世英雄少女心 2020-12-18 21:50

I have a simple C++ program that I thought would print out a double value defined in f and g as doubles ... but C++ is printing them out as integers. I checked the default

2条回答
  •  星月不相逢
    2020-12-18 22:19

    There are three floating-point notations:

    • std::fixed for fixed-point notation.
    • std::scientific for scientific notation.
    • (none) the default notation.

    The default notation just uses the precision value to count all digits of the number, not just the decimal part. If you use a short number (like 3.1415) you can see that the number is displayed correctly.

    It's also interesting to know, as cplusplus.com documentation cites, that:

    The default notation can be selected by calling str.unsetf(ios_base::floatfield).


    P.S.: for C++11 there are new notations: hexfloat for hexadecimal and defaultfloat for the so called (none) before.

提交回复
热议问题