Why does cout.setf(ios::fixed) change my floats to hexadecimal?

前端 未结 1 1557
死守一世寂寞
死守一世寂寞 2021-01-19 01:22

I experienced this weird issue recently having to do with cout.setf(ios::fixed). Took me quite a while to track down the cause and thought I\'d ask here to learn more.

相关标签:
1条回答
  • 2021-01-19 01:38

    Because you told it to.

    From setf documentation on cppreference.com:

    scientific - generate floating point types using scientific notation, or hex notation if combined with fixed: see std::scientific
    fixed - generate floating point types using fixed notation, or hex notation if combined with scientific: see std::fixed

    So, when setting std::fixed, you need to unset std::scientific (which is what your unmasking of std::floatfield does, because std::floatfield is std::scientific|std::fixed|(std::scientific|std::fixed)|0) to avoid the hex notation.

    0 讨论(0)
提交回复
热议问题