Formatting Numbers and Strings for Output

Formatting Numbers

Use the NumberFormat class:

http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html (Links to an external site.)

Integer formats

%d: will print the integer as it is.
%6d: will print the integer as it is. If the number of digits is less than 6, the output will be padded on the left.
%-6d: will print the integer as it is. If the number of digits is less than 6, the output will be padded on the right.
%06d: will print the integer as it is. If the number of digits is less than 6, the output will be padded on the left with zeroes.
%.2d: will print maximum 2 digits of the integer.

Currency formats

Time formats

Formatting Strings

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html (Links to an external site.) http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/ (Links to an external site.)

String formatting

%s: will print the string as it is.
%15s: will print the string as it is. If the string has less than 15 characters, the output will be padded on the left.
%-6s: will print the string as it is. If the string has less than 6 characters, the output will be padded on the right.
%.8d: will print maximum 8 characters of the string.

results matching ""

    No results matching ""