Java System.out.print格式化

4

以下是我的代码(部分代码)。我的问题是,我能否在前面添加“00”使前9个数字显示,并在10-99的数字前添加“0”。

我必须显示所有360个月付款,但如果月份数字长度不同,就会出现输出文件移动到右侧并偏移输出外观的情况。

System.out.print((x + 1) + "  ");  // the payment number
System.out.print(formatter.format(monthlyInterest) + "   ");    // round our interest rate
System.out.print(formatter.format(principleAmt) + "     ");
System.out.print(formatter.format(remainderAmt) + "     ");
System.out.println();

结果:

8              $951.23               $215.92         $198,301.22                         
9              $950.19               $216.95         $198,084.26                         
10              $949.15               $217.99         $197,866.27                         
11              $948.11               $219.04         $197,647.23  

我希望看到的是:

008              $951.23               $215.92         $198,301.22                         
009              $950.19               $216.95         $198,084.26                         
010              $949.15               $217.99         $197,866.27                         
011              $948.11               $219.04         $197,647.23  

你需要查看我这个类的哪些其他代码以便帮助呢?

6个回答

9

既然你已经在其他部分使用了格式化程序,那么只需使用DecimalFormat:

import java.text.DecimalFormat;

DecimalFormat xFormat = new DecimalFormat("000")
System.out.print(xFormat.format(x + 1) + " ");

另一种方法是使用printf在一整行中完成整个任务:

System.out.printf("%03d %s  %s    %s    \n",  x + 1, // the payment number
formatter.format(monthlyInterest),  // round our interest rate
formatter.format(principleAmt),
formatter.format(remainderAmt));

我所做的是使用: import java.text.DecimalFormat;然后我使用了Adam的建议并进行了一些编辑。谢谢Adam!DecimalFormat newFormat = new DecimalFormat("000");System.out.print(newFormat.format(x + 1) + " ");
008 $951.23 $215.92 $198,301.22
009 $950.19 $216.95 $198,084.26
010 $949.15 $217.99 $197,866.27
011 $948.11 $219.04 $197,647.23
- RazorSharp

5

由于您正在使用Java,printf从版本1.5开始可用。

您可以像这样使用它:

System.out.printf("%03d ", x);

例如:

System.out.printf("%03d ", 5);
System.out.printf("%03d ", 55);
System.out.printf("%03d ", 555);

将给您

005 055 555

作为输出

参见:System.out.printf格式化字符串语法


4

3

类似这样的内容

public void testPrintOut() {
    int val1 = 8;
    String val2 = "$951.23";
    String val3 = "$215.92";
    String val4 = "$198,301.22";
    System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));

    val1 = 9;
    val2 = "$950.19";
    val3 = "$216.95";
    val4 = "$198,084.26";
    System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));
}

0

只需使用\t进行缩进。

示例:

System.out.println(monthlyInterest + "\t")

//as far as the two 0 in front of it just use a if else statement. ex: 
x = x+1;
if (x < 10){
    System.out.println("00" +x);
}
else if( x < 100){
    System.out.println("0" +x);
}
else{
    System.out.println(x);
}

有其他的方法可以做到这一点,但这是最简单的方法。


0

你确定要使用 "055" 而不是 "55" 吗?一些程序将前导零解释为八进制,因此它会将 055 解释为 (十进制) 45 而不是 (十进制) 55。

这应该意味着删除 '0'(零填充)标志。

例如,将 System.out.printf("%03d ", x); 更改为更简单的 System.out.printf("%3d ", x);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接