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

后端 未结 6 1179
你的背包
你的背包 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:10

    Elliot's answer is of course correct, but for completeness' sake it's worth noting that if you don't want to print the value immediately, but instead hold the String for some other usage, you could use the DecimalFormat class:

    DecimalFormat df = new DecimalFormat("##.##%");
    double percent = (mark / tm);
    String formattedPercent = df.format(percent);
    

提交回复
热议问题