Printing the correct number of decimal points with cout

前端 未结 12 1508
孤独总比滥情好
孤独总比滥情好 2020-11-22 08:26

I have a list of float values and I want to print them with cout with 2 decimal places.

For example:

10.900  should be prin         


        
12条回答
  •  鱼传尺愫
    2020-11-22 08:51

    You have to set the 'float mode' to fixed.

    float num = 15.839;
    
    // this will output 15.84
    std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
    

提交回复
热议问题