How to 'cout' the correct number of decimal places of a double value?

前端 未结 9 1809
迷失自我
迷失自我 2020-11-27 07:44

I need help on keeping the precision of a double. If I assign a literal to a double, the actual value was truncated.

int main() {
    double x =         


        
9条回答
  •  攒了一身酷
    2020-11-27 08:15

    You must use setiosflags(ios::fixed) and setprecision(x).

    For example, cout << setiosflags(ios::fixed) << setprecision(4) << myNumber << endl;

    Also, don't forget to #include .

提交回复
热议问题