views:

120

answers:

2

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
%d wouldn't suffice if the value you are trying to print is long. In that case, you have to parse it.
Milli
@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
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
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