For instance, %11.2lf
in C++ becomes %11.2f
in Java. How about for long format?
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
%d means number. %0nd means zero-padded number with a length. You build n by subtraction in your example. %s is a string. Your format string ends up being this: "%03d%s", 0, "Apple"
The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: The value of i is: 461012.
As you may have worked out, it's not necessary to specify the l
flag. According to the docs, a decimal integer is specified by d
just like in C++. So the answer is just %d
.
Use %d
for decimals (long, int). It works OK. E.g.:
System.err.println(String.format("%d", 193874120937489387L));
...will print just fine. Read up on java.util.Formatter
for more details. %d
will take a long
, no problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With