Printing the correct number of decimal points with cout

前端 未结 12 1684
孤独总比滥情好
孤独总比滥情好 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:36

    I had this similar problem in a coding competition and this is how I handled it. Setting a precision of 2 to all double values

    First adding the header to use setprecision

    #include

    Then adding the following code in our main

      double answer=5.9999;
      double answer2=5.0000;
      cout<

    Output:

    5.99
    5.00
    

    You need to use fixed for writing 5.00 thats why,your output won't come for 5.00.

    A short reference video link I'm adding which is helpful

提交回复
热议问题