简体   繁体   English

在java帮助中格式化输出

[英]Formatting output in java assistance

Was wondering if anyone can further guide me as far as using width specifiers to making my output look like this using the static string format method ONLY. 想知道是否有人可以进一步指导我使用宽度说明符使我的输出看起来像这样只使用静态字符串格式方法。

ID: 1122 ID:1122

Created: 03/05/14 创建时间:03/05/14

Owner: Sam Williams 所有者:Sam Williams

Opening Balance: $14600.0 期初余额:14600.0美元

Annual Rate: 4.5 年费率:4.5

Withdraw: 5400.0 提款:5400.0

Current Balance: $14600.0 当前余额:14600.0美元

Currently, my output is this (using tabs and new lines) 目前,我的输出是这样的(使用标签和新行)

ID: 1122 ID:1122

Created: 03/05/14 创建时间:03/05/14

Owner: Sam Williams 所有者:Sam Williams

Opening Balance: $14600.0 期初余额:14600.0美元

Annual Rate: 4.5 年费率:4.5

Withdraw: 5400.0 提款:5400.0

Current Balance: $ 14600.0 当前余额:14600.0美元

JAVA CODE CURRENTLY IS: JAVA代码目前是:

System.out.println("ID:\t\t " + a.getId());
System.out.println("Created:\t" + String.format("%tD", a.getDateCreated()));
System.out.println("Owner:\t\t" + "" + a.getOwner() +
"\nOpening Balance: $" + a.getBalance() + "\nAnnual Rate:\t" + a.getAnnualRate() +"\nWithdraw:\t" + withDrawal +"\nCurrent Balance:\t" + " $ " + String.format("%.2f", a.getBalance()));

You can use format method in java. 您可以在java中使用format方法。 It looks something like: 它看起来像:

System.out.format("The Name is of %d is %f.%n", i, r);

format method 格式方法

Edited: The general syntax is: %[flags][width][.precision][argsize]typechar 编辑:一般语法是: %[flags][width][.precision][argsize]typechar

And it will work with all the datatypes. 它适用于所有数据类型。 It is an analogy of format specifier in C. 它类似于C中的格式说明符。

You'd better learn the basic usage of String formatting in Java like decimal, float, string, date and time . 您最好学习Java中字符串格式的基本用法,如十进制,浮点数,字符串,日期和时间。

For example you can do like this with your first line: 例如,您可以使用第一行执行此操作:

System.out.println(String.format("ID:%12d" , a.getId()));

Here 12 is the max length if id , if your length(id) < 12 then some space will be insert on left . 这里12是id的最大长度,如果你的长度(id)<12,那么左边会插入一些空格。

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

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