samedi 14 octobre 2017

Double with printf and aligning a line with printf in java

I made a random gradebook generator in java and I want to print the properties in a line regularly with printf. And I have a double variable which I can't print it with %d. Here is an example from my code:

double average;
ArrayList<Double> average1=new ArrayList<Double>();

//for loop
System.out.printf("%s%10s%10d%5d%5d%5d%5d%15f%10s\n",name1.get(j),lastname1.get(j),quiz1.get(j),quiz2.get(j),project.get(j),midterm.get(j),final.get(j),average.get(j),lettergrade.get(j));

First 2 line of output:

Ypu   MBQLCKU        33   53   54   20   81      48,000000         D
Pgkff       YDH         1   35   55   40   56      37,000000         D
//.....so on so forth

And I want to arrange them like:

Ypu     MBQLCKU      33  53   54   20   81      48,00         D
Pgkff   YDH          1   35   55   40   56      37,00         D

And %f gives me so many 0's after comma. I need exact double number to print like 57,34. What should I change in this left side of printf code?




Aucun commentaire:

Enregistrer un commentaire