For instance, %11.2lf
in C++ becomes %11.2f
in Java.
How about for long format?
C++ Equivalent of %ld in Java for String.format()
Asked Answered
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
.
%d wouldn't suffice if the value you are trying to print is long. In that case, you have to parse it. –
Judsonjudus
@Milli: While I'm not sure about the 1st sentence (don't think signedness is a consideration), I just confirmed experimentally that %d will correctly format longs! –
Chablis
You are RIGHT! My bad.. I had also String in the same statement with long.. The error was caused by the %d %d while it should have been %d %s. Thank you Andrzej! –
Judsonjudus
@CarlSmotricz is correct, signedness has nothing to do with it. I cleaned up the question to remove that. –
Inlier
String.format() will throw java.util.UnknownFormatConversionException at runtime if you use the 'l' flag with the 'd' flag (e.g. %ld) (demonstrated on Java 7). –
Arlyne
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.
WORKS PERFECT!! –
Orpah
© 2022 - 2024 — McMap. All rights reserved.