25

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

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

    mark= s.nextDouble();

    double percent= (mark / tm )*100  ;

    System.out.println("Student Name      : " +name );

    System.out.println("Percentage In Exam: " +percent+"%"); 

    System.out.println(" ");
}

I would like to format the percent value to 2 decimal places but since it's inside a while loop I cannot use the printf.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Vishal Jhoon
  • 255
  • 1
  • 3
  • 4

6 Answers6

19

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);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    This isn't very i18n friendly as in some languages you want a non breaking space between the number and the percent symbol. NumberFormat handles that out of the box, so Joe's answer is better on this regard: https://stackoverflow.com/a/55978509/2352699 – Fred Porciúncula Jan 03 '22 at 16:54
17

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.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • You can use [setRoundingMode](https://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#setRoundingMode%28java.math.RoundingMode%29) – gutenmorgenuhu Nov 16 '14 at 06:22
14
NumberFormat percentageFormat = NumberFormat.getPercentInstance();
percentageFormat.setMinimumFractionDigits(2);
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
  • Thanks for your answer. Can you explain how this works and why it's a good solution to the problem? – ggorlen May 04 '19 at 01:46
  • This solution uses the current Locale. In French speaking Canada they write percents like "100,00 %". This may or may not be what you want, who is reading your output a machine or a human? – satur9nine Jan 29 '20 at 23:02
  • 2
    To actually print something useful, you need to print your percentage as a string using a third line of code: percentageFormat.format(yourNum) – Shane Sepac Sep 15 '20 at 12:14
3

You can do it using String.format

System.out.println(String.format("%s%.2f%s","Percentage In Exam: " ,percent,"%"));
Diunicorn
  • 31
  • 2
0

ِEasiest way:

   System.out.println(Math.floor(percent*100)/100);
Xline
  • 804
  • 5
  • 13
0

It may be best to acquire your percent formatter through NumberFormat.getInstance(Locale locale) and using the setMinimumFractionDigits methods (and maybe the others).

Anthony Chuinard
  • 1,028
  • 10
  • 17