Maybe did you already see some different print options.
The three different print options in the class PrintStream are:
- println
- printf
Println( … ) / Print( … )
The println("This is an example.") method prints: "This is an example." and moves the cursor to a new line. In this case is the output a string, but the string could also be from the type int and double.
The print("This is an example.") method instead prints just the string "This is an example.", but does not move the cursor to a new line. In this case is the output a string, but the string could also be from the type int and double.
println()/print is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left/right/etc..-justified) ), then printf would be used.
printf(” … “)
printf gives you the option to print a variable in a piece of text. For example:
output: Give the number we want is 10.
A summary of printf format specifiers
Here’s a quick summary of the available printf
format specifiers:
Specifier | Type |
---|---|
%c | character |
%d | integer |
%f | floating-point number |
%s | a string of characters |
%% | print a percent sign |
%e | exponential floating-point number |
Right-justifying printf
To move the output to the right, fill in the number you want right-align plus the specifier. In the example below, we use 3 but you could enter another number; 5, 6, 90, 100, and so on..
Code | Result |
---|---|
printf(“%3d”, 0); | 0 |
printf(“%5d”, 678); | 678 |
printf(“%10d”, -10); | -10 |
Left-justifying printf
To move the output to the left, fill in the number you want left-allign plus the specifier. In the example below we use 3 but you could enter another number; 5, 6, 90, 100, and so on..
This looks weirth because there are no more places on the left side, but in fact this could be useful when you are making labels
Code | Result |
---|---|
printf(“%-3d”, 0); | ‘0 ‘ |
printf(“%-5d”, 678); | ‘678 ‘ |
printf(“%-20d”, -10); | ‘-10 ‘ |
For more information about left (and right) aligning look at this video:
The printf integer zero-fill option
To zero-fill your printf
integer output just adds a zero at the places where there is no character. So, when you have two numbers/characters (the number 22 for example) then will there be a zero before the 22, like this 022.
Code | Result |
---|---|
printf(“%03d”, 1); | 001 |
printf(“%05d”, 678); | 00678 |
printf(“%04d”, -10); | -010 |
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 |
---|---|---|
right aligning | printf(“‘%5d'”, 5); | 5 |
left right aligning | printf(“‘%-5d'”, 5); | 5 |
right aligning and zero-filled | printf(“‘%05d'”, 5); | 0005 |
right aligning and a plus sign | printf(“‘%+5d'”, 5); | +5 |
right aligning, plus sign and right aligning | printf(“‘%-+5d'”, 5); | +5 |
formatting floating point numbers with printf
Here are several examples showing how to format floating-point numbers with printf
:
Description | Code | Result |
---|---|---|
One position after the decimal | printf(“‘%.1f'”, 99.5678); | ‘99.6’ |
Two positions after the decimal | printf(“‘%.2f'”, 99.5678); | ‘99.57’ |
Ten-wide, two positions after the decimal | printf(“‘%10.2f'”, 99.5678); | ‘ 99.57’ |
Ten-wide, two positions after the decimal, zero-filled | printf(“‘%010.2f'”, 99.5678); | ‘0000010.35’ |
Ten-wide, two positions after the decimal, left-justified | printf(“‘%–10.2f'”, 99.5678); | ‘99.57 ‘ |
printf string formatting
Here are several examples that show how to format string output with printf
:
Description | Code | Result |
---|---|---|
A simple string | printf(“‘%s'”, “word”); | ‘word’ |
A string with a minimum length | printf(“‘%15s'”, “word”); | ‘ word’ |
Minimum length, left-justified | printf(“‘%-15s'”, “word”); | ‘word ‘ |
printf special characters
The following character sequences have 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 |