gpt4 book ai didi

java - 尝试使用 Java 中的 printf() 格式化发票

转载 作者:行者123 更新时间:2023-12-02 02:32:19 25 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 printf() 格式化发票。我希望最终结果应该是这样的。

_______________________________________
Invoice 1
24-Oct-2017
---------------------------------------
| ID | DESC | AMT |
---------------------------------------
| 1 | Item1 | 15,000|
---------------------------------------
| 2 | Item2 | 200,000|
---------------------------------------
| 3 | Enterprise Javaaaaaaa | |
| | a | 300,000|
---------------------------------------
| Sub Total 515,000|
---------------------------------------
| Tax(5%) 25,750|
---------------------------------------
| Total 540,750|
---------------------------------------
THANK YOU!
_______________________________________

不知何故,我达到了预期的结果,但是当项目的描述长度超过单元格的空间时,它会将边框推到右侧,而不是换行。

    _______________________________________
Invoice 1
24-Oct-2017
---------------------------------------
| ID | DESC | AMT |
---------------------------------------
| 1 | Item1 | 15,000|
---------------------------------------
| 2 | Item2 | 200,000|
---------------------------------------
| 3 | Enterprise Javaaaaaaaaa | 300,000|
---------------------------------------
| Sub Total 515,000|
---------------------------------------
| Tax(5%) 25,750|
---------------------------------------
| Total 540,750|
---------------------------------------
THANK YOU!
_______________________________________

这是这些行的格式字符串。

public void printTitles(){
Date todayDate = new Date();
System.out.printf("_______________________________________");
System.out.printf("\n\t\t\tInvoice 1");
System.out.printf("\n\t\t\t\t%td-%<tb-%<tY", todayDate);
}

public void printHeaders(){
System.out.printf("\n---------------------------------------");
System.out.printf("\n| ID |\t\t\tDESC\t\t|\tAMT\t |");
System.out.printf("\n---------------------------------------");
}

public void printRows(String id, String desc, int fee) {
System.out.printf("\n| %s | %.20s| %,8d|", id, desc, fee);
System.out.printf("\n---------------------------------------");
}

public void printResult() {
System.out.printf("\n|\t Sub Total\t\t %,8d|",subTotal);
System.out.printf("\n---------------------------------------");
System.out.printf("\n|\t Tax(5%%)\t\t\t\t %,8d|", tax);
System.out.printf("\n---------------------------------------");
System.out.printf("\n|\t Total\t\t\t\t %,4d|", total);
System.out.printf("\n---------------------------------------");
System.out.printf("\n\t\t\t\tTHANK YOU!");
System.out.printf("\n_______________________________________");
}

如何让描述自动变得比包含单元格的宽度长?

最佳答案

public static void printRows(String id, String desc, int fee) {
System.out.printf("\n| %s | %-20.20s| %,8d|", id, desc, fee);
while(desc.length()>20){
desc=desc.substring(20);
System.out.printf("\n| | %-20.20s| |", desc);
}
System.out.printf("\n---------------------------------------");
}

(我必须承认,-20部分来自Matt的回答)

关于java - 尝试使用 Java 中的 printf() 格式化发票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46907351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com