What is the role of **std::setprecision()** without **std::fixed** in c++?

前端 未结 4 1038
陌清茗
陌清茗 2021-02-04 15:52

As shown in the tutorial http://www.cplusplus.com/reference/iomanip/setprecision/

// setprecision example
#include      // std::cout, std::fixed
         


        
4条回答
  •  無奈伤痛
    2021-02-04 16:12

    According to http://en.cppreference.com/w/cpp/io/ios_base/precision, the precision decides how many digits are printed, not how many digits after the floating point are printed:

    std::ios_base::precision

    Manages the precision (i.e. how many digits are generated) of floating point output

    This explains the rounding.

    Yes, using std::fixed will change the interpretation of the floatfield precision. According to http://www.cplusplus.com/reference/ios/ios_base/precision/:

    In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.

提交回复
热议问题