For instance, "%11.2lf" in C++ becomes "%11.2f" in Java. How about for long format?
+2
A:
As you may have worked out, it's not necessary to specify the l
flag since Java data types don't have optional signedness.
According to the docs, a decimal integer is specified by d
just like in C++. So the answer is just %d
.
Andrzej Doyle
2010-01-06 08:45:56
%d wouldn't suffice if the value you are trying to print is long. In that case, you have to parse it.
Milli
2010-01-06 08:47:14
@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!
Carl Smotricz
2010-01-06 08:51:09
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!
Milli
2010-01-06 08:58:55
A:
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.
Stu Thompson
2010-01-06 08:58:16