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

前端 未结 4 983
陌清茗
陌清茗 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:22

    The meaning of precision depends on whether fixed, scientific or default format is used. For fixed format, it's the number of digits after the decimal point. For scientific, it is, too - but there's always exactly one digit before the point; the exponent is used to shift the decimal point into position. For default format, precision specifies the total number of digits printed (roughly; it's a bit more complicated).

    For details, see http://en.cppreference.com/w/cpp/io/c/fprintf , in particular the description of %f, %e and %g format specifiers (for fixed, scientific and default format, correspondingly). std::cout << f is specified to behave as if printf("%.", f) is called (to the first approximation; the reality is, again, a bit more complicated, but the details are not relevant to the issue at hand), where is the number specified by setprecision, and is one of f, e or g.

提交回复
热议问题