繁体   English   中英

“ String#format”模式的含义是什么?

[英]What is the meaning of this 'String#format' pattern?

在研究任务(创建收据)的过程中,我找到了以下代码:

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 EUR"吗?

我认为这是代码中所说的部分:

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

但是为什么呢?

String.format用于格式化字符串。

%是一个特殊字符,表示遵循格式化指令。

参数的类型由dfxs

  • 整数-d,

  • 字符串-s,

  • 浮点数-f

  • 十六进制格式-x的整数。

%-9s :将按%-9s格式化字符串。 如果字符串少于9个字符,则将在右侧填充输出。

%2d :将按原样格式化整数。 如果位数小于2,则输出将在左侧填充。

%5.2f :最多将格式化该数字的2个十进制数字。 输出将至少占用5个字符。 如果位数不足,将被填充。

暂无
暂无

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

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