Java 这个';字符串#格式';图案
在研究任务(创建收据)期间,我发现以下代码:Java 这个';字符串#格式';图案,java,receipt,Java,Receipt,在研究任务(创建收据)期间,我发现以下代码: public class Kassenzettel { public static void main(String[] args) { // Einzelpreise - Waren double SEBox = 299.00; double Hauptplatine = 79.80; double Erweiterungsplatine =52.60; do
public class Kassenzettel {
public static void main(String[] args) {
// Einzelpreise - Waren
double SEBox = 299.00;
double Hauptplatine = 79.80;
double Erweiterungsplatine =52.60;
double Anschlusskabel = 19.50;
double Firmware = 36.00;
// Anzahl - Waren
int anzSEBox = 1;
int anzHauptplatine = 1;
int anzErweiterungsplatine = 1;
int anzAnschlusskabel = 2;
int anzFirmware = 2;
// Inhalt - Brieftasche
double brieftasche = 550.00;
// Gekaufte Waren
double summe = 0;
summe = summe + anzSEBox * SEBox;
summe = summe + anzHauptplatine * Hauptplatine;
summe = summe + anzErweiterungsplatine * Erweiterungsplatine;
summe = summe + anzAnschlusskabel * Anschlusskabel;
summe = summe + anzFirmware * Firmware;
if (summe > brieftasche) {
System.out.println("Nicht genuegend Geld in der Brieftasche");
} else {
System.out.println("____________________________________");
System.out.println("DATCOM protelematik GmbH - Kassenbon");
System.out.println("____________________________________");
System.out.println(String.format("%-9s %2d x %5.2f EUR", "SEBox", anzSEBox, SEBox));
System.out.println(String.format("%30.2f EUR", anzSEBox * SEBox));
System.out.println(String.format("%-9s %2d x %5.2f EUR", "Hauptplatine", anzHauptplatine, Hauptplatine));
System.out.println(String.format("%30.2f EUR", anzHauptplatine * Hauptplatine));
System.out.println(String.format("%-9s %2d x %5.2f EUR", "Erweiterungsplatine", anzErweiterungsplatine, Erweiterungsplatine));
System.out.println(String.format("%30.2f EUR", anzErweiterungsplatine * Erweiterungsplatine));
System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel));
System.out.println(String.format("%30.2f EUR", anzAnschlusskabel * Anschlusskabel));
System.out.println(String.format("%-9s %2d x %5.2f EUR", "Firmware", anzFirmware, Firmware));
System.out.println(String.format("%30.2f EUR", anzFirmware * Firmware));
System.out.println("____________________________________");
System.out.println(String.format("%-9s %20.2f EUR", "Gesamt", summe));
System.out.println(String.format("%-9s %20.2f EUR", "Gegeben", brieftasche));
System.out.println();
System.out.println(String.format("%-9s %20.2f EUR", "Zurueck", brieftasche - summe));
}
}
}
但我不明白下面这句话:
System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel));
我知道System.out.println是做什么用的,但有人能给我解释一下这到底是做什么用的:“%-9s%2d x%5.2f欧元”
我认为这是针对代码中说:
SEBox 1 x 299,00 EUR
299,00 EUR
Hauptplatine 1 x 79,80 EUR
79,80 EUR
Erweiterungsplatine 1 x 52,60 EUR
52,60 EUR
Anschlusskabel 2 x 19,50 EUR
39,00 EUR
Firmware 2 x 36,00 EUR
72,00 EUR
但是为什么以及如何?用于格式化字符串
%
是一个特殊字符,表示后面跟着格式化指令
参数类型由d
、f
、x
和s
表示
- 整数-d
- 字符串-s
- 浮点-f
- 十六进制格式的整数-x
%-9s
:将按原样格式化字符串。如果字符串少于9个字符,输出将在右侧填充
%2d
:将按原样格式化整数。如果位数小于2,输出将在左侧填充
%5.2f
:将格式化数字的最大两位十进制数字。输出将至少占用5个字符。如果位数不够,将对其进行填充