Java System.out.print 格式化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9849800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java System.out.print formatting
提问by RazorSharp
Here is my code (well, some of it). The question I have is, can I get the first 9 numbers to show with a leading 00 and numbers 10 - 99 with a leading 0.
这是我的代码(好吧,其中一些)。我的问题是,我能否获得以 00 开头的前 9 个数字和以 0 开头的数字 10 - 99。
I have to show all of the 360 monthly payments, but if I don't have all month numbers at the same length, then I end up with an output file that keeps moving to the right and offsetting the look of the output.
我必须显示所有 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();
Results:
结果:
8 1.23 5.92 8,301.22
9 0.19 6.95 8,084.26
10 9.15 7.99 7,866.27
11 8.11 9.04 7,647.23
What I want to see is:
我想看到的是:
008 1.23 5.92 8,301.22
009 0.19 6.95 8,084.26
010 9.15 7.99 7,866.27
011 8.11 9.04 7,647.23
What other code do you need to see from my class that could help?
您还需要从我的课程中看到哪些可以提供帮助的其他代码?
采纳答案by Adam
Since you're using formatters for the rest of it, just use DecimalFormat:
由于您在其余部分使用格式化程序,因此只需使用 DecimalFormat:
import java.text.DecimalFormat;
DecimalFormat xFormat = new DecimalFormat("000")
System.out.print(xFormat.format(x + 1) + " ");
Alternative you could do whole job in whole line using printf:
或者,您可以使用 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));
回答by Hari Menon
回答by Jomoos
Since you are using Java, printf
is available from version 1.5
由于您使用的是 Java,printf
因此可从 1.5 版开始使用
You may use it like this
你可以这样使用它
System.out.printf("%03d ", x);
System.out.printf("%03d ", x);
For Example:
例如:
System.out.printf("%03d ", 5);
System.out.printf("%03d ", 55);
System.out.printf("%03d ", 555);
Will Give You
会给你
005 055 555
005 055 555
as output
作为输出
回答by Xuvi
Something likes this
喜欢这个
public void testPrintOut() {
int val1 = 8;
String val2 = "1.23";
String val3 = "5.92";
String val4 = "8,301.22";
System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));
val1 = 9;
val2 = "0.19";
val3 = "6.95";
val4 = "8,084.26";
System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));
}
回答by user2784358
Are you sure that you want "055" as opposed to "55"? Some programs interpret a leading zero as meaning octal, so that it would read 055 as (decimal) 45 instead of (decimal) 55.
您确定要“055”而不是“55”吗?一些程序将前导零解释为八进制,因此它将 055 读作(十进制)45 而不是(十进制)55。
That should just mean dropping the '0' (zero-fill) flag.
这应该只是意味着删除“0”(零填充)标志。
e.g., change System.out.printf("%03d ", x);
to the simpler System.out.printf("%3d ", x);
例如,更改System.out.printf("%03d ", x);
为更简单的System.out.printf("%3d ", x);
回答by michael hocking
Just use \t
to space it.
只是\t
用来隔开它。
Example:
例子:
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);
}
There are other ways to do it, but this is the simplest.
还有其他方法可以做到这一点,但这是最简单的。