0

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?

pjs
  • 18,696
  • 4
  • 27
  • 56
İlker Çetin
  • 51
  • 1
  • 9

2 Answers2

0

First, let the string start with %7s instead of %s (so you align MBQ... and YDH even though Ypu and Pgkff are of different lengths).

Then see How to display an output of float data with 2 decimal places in Java? on how to fix your double trouble.

Stefan
  • 2,395
  • 4
  • 15
  • 32
0

If you want to restrict numbers that come after decimal point use the following formatter:

 double y=25.1299;
 System.out.printf("y=%.2f ",y);

Where number 2 gives you 2 spaces for numbers that come after the decimal point.

Pavisa
  • 102
  • 7