A printf Format Reference page (cheat sheet)

Source: Internet
Author: User
Tags java printf

Summary:this page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it is here.

A cool thing about the formatting syntax was that the specifiers you can use be printf very similar, if not identical, BETW Een different languages, including C, C + +, Java, Perl, Ruby, Scala, and others. So your printf knowledge are reusable, which is a good thing.

printf formatting with Perl and Java

In this cheat sheet I'll show all the examples using Perl, but at first it might help to see one example using both Perl a nd Java. So, here's a simple Perl printf example to get us started:

printf ("The%s jumped over the%s,%d times", "cow", "Moon", 2);

And here is three different Java printf examples, using different methods that is available to your in the Java Programmi Ng language:

System.out.format ("The%s jumped over the%s,%d times", "cow", "Moon", 2); System.err.format ("The%s jumped over the%s,%d times", "cow", "Moon", 2); String result = String.Format ("The%s jumped over the%s,%d times", "cow", "Moon", 2);

As you can see in so last String.format example, that's line of code doesn ' t print any output, while the first line prints to Stan Dard output, and the second line prints to standard error.

In the remainder of this document I'll use Perl examples, but again, the actual format specifier strings can is used in MA NY different languages.

printf Format specifiers-summary

Here's a quick summary of the available printf format specifiers:

%c Character
%d Decimal (integer) number (base 10)
%e Exponential floating-point number
%f Floating-point number
%i Integer (base 10)
%o Octal number (base 8)
%s A string of characters
%u unsigned decimal (integer) number
%x Number in hexadecimal (base 16)
%% Print a percent sign
\% Print a percent sign
Controlling integer width with printf

The %3d specifier means a minimum width of three spaces, which, by default, would be right-justified:

printf ("%3d", 0); 0
printf ("%3d", 123456789); 123456789
printf ("%3d",-10); -10
printf ("%3d",-123456789); -123456789
left-justifying printf Integer Output

To left-justify integer output with printf, just add a minus sign ( - ) after % the symbol, such as this:

printf ("%-3d", 0); 0
printf ("%-3d", 123456789); 123456789
printf ("%-3d",-10); -10
printf ("%-3d",-123456789); -123456789
The printf zero-fill option

To zero-fill your printf integer output, just add a zero ( 0 ) after % the symbol, like this:

printf ("%03d", 0); 000
printf ("%03d", 1); 001
printf ("%03d", 123456789); 123456789
printf ("%03d",-10); -10
printf ("%03d",-123456789); -123456789
printf integer formatting

As a summary of printf integer formatting, here's a little collection of integer formatting examples. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.

Description Code Result
At least five wide printf ("'%5d '", 10); ' 10 '
At least Five-wide, left-justified printf ("'%-5d '", 10); ' 10 '
At least Five-wide, zero-filled printf ("'%05d '", 10); ' 00010 '
At least five-wide, with a plus sign printf ("'%+5d '", 10); ' +10 '
Five-wide, plus sign, left-justified printf ("'%-+5d '", 10); ' +10 '
Printf-floating Point numbers

Here is several examples showing how to format floating-point numbers with printf:

Description Code Result
Print one position after the decimal printf ("'%.1f '", 10.3456); ' 10.3 '
Positions after the decimal printf ("'%.2f '", 10.3456); ' 10.35 '
Eight-wide, positions after the decimal printf ("'%8.2f '", 10.3456); ' 10.35 '
Eight-wide, four positions after the decimal printf ("'%8.4f '", 10.3456); ' 10.3456 '
Eight-wide, positions after the decimal, zero-filled printf ("'%08.2f '", 10.3456); ' 00010.35 '
Eight-wide, positions after the decimal, left-justified printf ("'%-8.2f '", 10.3456); ' 10.35 '
Printing a much larger number with the That same format printf ("'%-8.2f '", 101234567.3456); ' 101234567.35 '
printf string Formatting

Here is several examples that show how to format string output with printf:

Description Code Result
A Simple string printf ("'%s '", "Hello"); ' Hello '
A string with a minimum length printf ("'%10s '", "Hello"); ' Hello '
Minimum length, left-justified printf ("'%-10s '", "Hello"); ' Hello '
Summary of Special printf characters

The following character sequences has a special meaning when used as printf format specifiers:

\a Audible alert
\b Backspace
\f Form Feed
\ n NewLine, or linefeed
\ r Carriage return
\ t tab
\v Vertical tab
\\ Backslash

As you can see from that last example, because the backslash character itself are treated specially, you has to print Backslash characters in a row to get one backslash character to appear in your output.

Here is a few examples of how to use these special characters:

Description Code Result
Insert a tab character in a string printf ("Hello\tworld"); Hello World
Insert a newline character in a string printf ("Hello\nworld"); Hello
World
Typical use of the newline character printf ("Hello world\n"); Hello World
A dos/windows path with backslash characters printf ("c:\\windows\\system32\\"); C:\Windows\System32\

A printf Format Reference page (cheat sheet)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.