Galipan
Galipan

Reputation: 63

How do I control the alignment of formatted text in Java?

I'm knocking my head here using System.out.printf and I really don't understand something that seems that it should be much more simple.

public class main{
    public static void main(String args[]){
        double dog  = 3.85;
        double cat = 333.85;
        System.out.printf("Hola:%7.1f\n",dog);
        System.out.printf("Hola:%5.0f", cat);
    }
}

I'm trying to understand what the significance is of the numbers after the percentage symbol. In short in the line:

System.out.print("Number: %x.y", someNumber);

what does the x tell me and what does the y tell me?

Upvotes: 0

Views: 112

Answers (1)

chubbsondubs
chubbsondubs

Reputation: 38706

  • x = minimum width of how many digits are displayed to the left of the decimal with padding.
  • y = precision of floating point numbers to the right of the decimal.

Upvotes: 1

Related Questions