8
I’m seeing a java arrays exercises, but I can’t understand what the %3d
, if it’s a int
.
for (i=0; i<n; i++) {
System.out.printf("a[%d] = %3d b[%d] = %3d\n", i, a[i], i, b[i]);
}
8
I’m seeing a java arrays exercises, but I can’t understand what the %3d
, if it’s a int
.
for (i=0; i<n; i++) {
System.out.printf("a[%d] = %3d b[%d] = %3d\n", i, a[i], i, b[i]);
}
10
The %d
is used to format whole variables, basically can be used as follows:
%d
- formats an entire variable with how many digits there are;
%4d
- formats an entire variable with how many digits there are, but if the variable has a quantity less than 4 digits, fills the missing ones with white space on the left;
%04d
- formats an entire variable with how many digits there are, but if the variable has a quantity less than 4 digits, fills the missing ones with zeros on the left.
Examples:
%d
applied to 1234 would stay 1234;
%4d
applied to 1234 would be 1234, but applied to 123 would be _123 (where _ is a blank);
%04d
applied to 123 stay 0123.
Take an example: http://ideone.com/0JxzzH
6
The explanation of %3d
can be divided as below:
%
prints a variable here,3
use at least 3 characters to display, filling in spaces if necessary,d
the variable is of the int
.See a demo on Ideone.
Browser other questions tagged java printf
You are not signed in. Login or sign up in order to post.
The %d I understood, but the '3', it is used to give a blank space then?
– user39733
That’s right. I updated my reply with a link.
– cantoni
3 is used to define how many white spaces will be given in an integer with less than 3 digits.
– cantoni
Simmm, less than 3d %
– user39733