Skip to content

robertbenson/printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

printf using Java

Quick Introduction - 5 minute guide

This is not by any means a comprehensive list of what printf can do. It is just a quick reference to the most used functions for my use cases. Refer to your preferred Java reference guide for a full overview of printf.

In Java, printf is a method used for formatted output. It is part of the PrintStream class and is typically used to format and print data to the console. The printf method allows you to specify a format string, which can include placeholders for different data types (e.g., integers, floating-point numbers, strings), you provide the values to be inserted into the placeholders.

printf(format, arguments);

format is the fortmatted string, arguments are the values that are provided to the formatted string.

%[index][flags][width][.precision]conversion-character

Conversion Characters:

Conversion characters are used to specify the type of data that will be inserted into the formatted string.

b : Boolean value.
B : Boolean value, uppercase.
d : decimal integer   [byte, short, int, long] 
f : floating-point number    [float, double] 
c : character Capital C will make uppercase
s : String  Capital S will make uppercase
h : hashcode   
n : use %n instead of \n

java_printf.png

output:

' 42.54'

placeholder %

%n is the same as \n, ie newline

In the printf function, the % character is used as a format specifier to indicate where and how to insert values into the formatted string

e.g. "Hello %s %s"; has two placeholders, that are strings.

%d is used as a placeholder for an integer,

%f for a floating-point number,

%s for a string, and so on.

System.out.printf("Hello %s %s", "foo", "bar"); foo is allocated to the first placeholder, bar is allocated to the second.

output

Hello foo bar

System.out.printf(format, arguments);

String format = "Hello %s%n";    // placeholder for a string  %n is newline
Object arguments = "World";
System.out.printf(format, arguments);

output: Hello World

The order of arguments, default or index:

default

int intValue = 128;
double doubleValue = 3.141;
String stringValue = "foo";

// Using %d for integer, %f for floating-point, %s for string
System.out.printf("Integer: %d, Floating-point: %.2f, String: %s%n", intValue, doubleValue, stringValue);   // the double value has 2 dp precision added .2

output: Integer: 128, Floating-point: 3.14, String: foo

index

specify explicitly, using an index, which argument to use with placeholder:

System.out.printf("%3$d  %4$d  %2$d  %1$d\n", 1,  2,  3,  4);     //3,4,2,1
System.out.printf("%4$d  %3$d  %1$d  %2$d\n", 1,  2,  3,  4);     //4,3,1,2

output:

3  4  2  1
4  3  1  2

System.out.printf(locale, format, arguments);

locale

A locale represents a specic geographical, political or cultural region.

It is primarily used for formatting and parsing dates, numbers in a locale-sensitive manner.

locale amount

The same amount, displayed in locale-sensitive format;

double amount = 2456220.156;
printfLocaleAmount(Locale.UK, amount);
printfLocaleAmount(Locale.FRANCE, amount);
printfLocaleAmount(Locale.GERMANY, amount);
printfLocaleAmount(Locale.ITALY, amount);
private static void printfLocaleAmount(Locale locale, double amount) {
    System.out.printf(locale, "locale amount in %s is %,.2f%n", locale.getCountry(), amount);
}

output:

locale amount in GB is 2,456,220.16
locale amount in FR is 2 456 220,16
locale amount in DE is 2.456.220,16
locale amount in IT is 2.456.220,16

locale date

Date date = new Date();
printfLocaleDate(Locale.UK,date);
printfLocaleDate(Locale.FRANCE,date);
printfLocaleDate(Locale.GERMANY,date);
printfLocaleDate(Locale.ITALY,date);
private static void printfLocaleDate(Locale locale, Date date) {
    System.out.printf(locale, "locale date in %s is %tc%n", locale.getCountry(), date);
}

output:

The same date, displayed in locale-sensitive format;

locale date in GB is Wed Dec 20 10:54:27 GMT 2023
locale date in FR is mer. déc. 20 10:54:27 GMT 2023
locale date in DE is Mi. Dez. 20 10:54:27 GMT 2023
locale date in IT is mer dic 20 10:54:27 GMT 2023

flags

Flags are used within the format string to modify the output format.

- left-justify ( default is right-justify )  
+ output a plus (+) or minus (-) sign for numerics
0 forces numerical values to be zero-padded otherwise blank padded
, comma grouping separator for numbers > 1000   

About

printf examples using Java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages