简体   繁体   English

如何控制Java中格式化文本的对齐方式?

[英]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. 我在这里用System.out.printf敲了敲头,我真的不明白似乎应该更简单的东西。

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? x告诉我什么,y告诉我什么?

  • x = minimum width of how many digits are displayed to the left of the decimal with padding. x =用填充在小数点左边显示几位数的最小宽度。
  • y = precision of floating point numbers to the right of the decimal. y =小数点右边的浮点数的精度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM