【问题标题】:What is the meaning of this 'String#format' pattern?这个“String#format”模式的含义是什么?
【发布时间】:2017-01-06 11:59:27
【问题描述】:

在研究任务(创建收据)期间,我发现了以下代码:

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

但是为什么以及如何?

【问题讨论】:

标签: java receipt


【解决方案1】:

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

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

参数类型用dfxs表示。

  • 整数 - d,

  • 字符串-s,

  • 浮点-f

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

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

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

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

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多