Reputation: 501
I'm looking at a code line similar to:
sprintf(buffer,"%02d:%02d:%02d",hour,minute,second);
I think the symbolic strings refer to the number of numeric characters displayed per hour, minute etc - or something like that, I am not entirely certain.
Normally I can figure this sort of thing out but I have been unable to find any useful reference searching "%02d %01d" on google. Anyone able to shed some light on this for me?
Upvotes: 49
Views: 189881
Reputation: 14628
https://cplusplus.com/reference/cstdio/printf/
the same rules should apply to Java.
in your case it means output of integer values in 2 or more digits, the first being zero if number less than or equal to 9
Upvotes: 32
Reputation: 429
% is a special character you put in format strings, for example in C language printf and scanf (and family), that basically says "this is a placeholder for something else, not to be printed/read literally."
For example, a
(%02d)
in a format string for printf is a placeholder for an integer variable that will be printed in decimal(%02d)
and padded to at least two digits, with zeros if necessary.The actual integer is supplied by you in an an argument to the function, e.g.
printf("%02d",5);
would print05
on screen,(%01d)
also works similarly
Same hold for Java and other languages too :)
Upvotes: 2
Reputation: 383866
They are formatting String
. The Java specific syntax is given in java.util.Formatter
.
The general syntax is as follows:
%[argument_index$][flags][width][.precision]conversion
%02d
performs decimal integer conversion d
, formatted with zero padding (0
flag), with width 2
. Thus, an int
argument whose value is say 7
, will be formatted into "07"
as a String
.
You may also see this formatting string in e.g. String.format
.
These are just some commonly used formats and doesn't cover the syntax exhaustively.
System.out.printf("Agent %03d to the rescue!", 7);
// Agent 007 to the rescue!
You can use the -
flag for left justification; otherwise it'll be right justification.
for (Map.Entry<Object,Object> prop : System.getProperties().entrySet()) {
System.out.printf("%-30s : %50s%n", prop.getKey(), prop.getValue());
}
This prints something like:
java.version : 1.6.0_07
java.vm.name : Java HotSpot(TM) Client VM
java.vm.vendor : Sun Microsystems Inc.
java.vm.specification.name : Java Virtual Machine Specification
java.runtime.name : Java(TM) SE Runtime Environment
java.vendor.url : http://java.sun.com/
For more powerful message formatting, you can use java.text.MessageFormat
. %n
is the newline conversion (see below).
System.out.println(Integer.toHexString(255));
// ff
System.out.printf("%d is %<08X", 255);
// 255 is 000000FF
Note that this also uses the <
relative indexing (see below).
System.out.printf("%+,010.2f%n", 1234.567);
System.out.printf("%+,010.2f%n", -66.6666);
// +01,234.57
// -000066.67
For more powerful floating point formatting, use DecimalFormat
instead.
%n
for platform-specific line separatorSystem.out.printf("%s,%n%s%n", "Hello", "World");
// Hello,
// World
%%
for an actual %
-signSystem.out.printf("It's %s%% guaranteed!", 99.99);
// It's 99.99% guaranteed!
Note that the double
literal 99.99
is autoboxed to Double
, on which a string conversion using toString()
is defined.
n$
for explicit argument indexingSystem.out.printf("%1$s! %1$s %2$s! %1$s %2$s %3$s!",
"Du", "hast", "mich"
);
// Du! Du hast! Du hast mich!
<
for relative indexingSystem.out.format("%s?! %<S?!?!?", "Who's your daddy");
// Who's your daddy?! WHO'S YOUR DADDY?!?!?
MessageFormat
with exampleUpvotes: 39
Reputation: 13776
http://en.wikipedia.org/wiki/Printf#printf_format_placeholders
The article is about the class of printf functions, in several languages, from the 50s to this day.
Upvotes: 0
Reputation: 9372
Instead of Googling for %02d
you should have been searching for sprintf()
function.
%02d
means "format the integer with 2 digits, left padding it with zeroes", so:
Format Data Result %02d 1 01 %02d 11 11
Upvotes: 80
Reputation: 3320
The answer from Alexander refers to complete docs...
Your simple example from the question simply prints out these values with 2 digits - appending leading 0 if necessary.
Upvotes: 0