Java System.out.print格式

Java System.out.print格式,java,format,Java,Format,这是我的代码(嗯,其中一些代码)。我的问题是,我能得到前9个数字以0开头和10-99以0开头吗 我必须显示所有360个月的付款,但是如果我没有相同长度的所有月数,那么我最终会得到一个输出文件,该文件会一直向右移动,并抵消输出的外观 System.out.print((x + 1) + " "); // the payment number System.out.print(formatter.format(monthlyInterest) + " "); // round our

这是我的代码(嗯,其中一些代码)。我的问题是,我能得到前9个数字以0开头和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  

您还需要从我的类中看到哪些代码可以提供帮助?

使用

因为您使用的是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

作为输出


请参阅:和

由于其余部分都使用格式化程序,只需使用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));
像这样的东西

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));
}

您确定要“055”而不是“55”吗?一些程序将前导零解释为八进制,因此它将055读为(十进制)45,而不是(十进制)55

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


e、 例如,更改
System.out.printf(“%03d”,x)
到更简单的
系统.out.printf(“%3d”,x)

只需使用
\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);
}
System.out.println(monthlyInterest+“\t”)
//对于前面的两个0,只需使用if-else语句。前任:
x=x+1;
if(x<10){
系统输出打印项次(“00”+x);
}
否则如果(x<100){
系统输出打印项次(“0”+x);
}
否则{
系统输出println(x);
}

还有其他方法,但这是最简单的。

我所做的就是:导入java.text.DecimalFormat;然后我用亚当的建议做了一些编辑。谢谢你,亚当!DecimalFormat newFormat=新的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