How do I format my percent variable to 2 decimal places?

后端 未结 6 1274
你的背包
你的背包 2021-02-05 09:51

This program is basically working with text files, reading the data & performing functions:

while(s.hasNext()){
    name= s.next();

    mark= s.nextDouble()         


        
6条回答
  •  没有蜡笔的小新
    2021-02-05 10:18

    You could use formatted output like,

    System.out.printf("Percentage In Exam: %.2f%%%n", percent);
    

    The Formatter syntax describes precision as

    Precision

    For general argument types, the precision is the maximum number of characters to be written to the output.

    For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator. If the conversion is 'g' or 'G', then the precision is the total number of digits in the resulting magnitude after rounding. If the conversion is 'a' or 'A', then the precision must not be specified.

    The double percent %% becomes a percent literal, and the %n is a newline.

提交回复
热议问题