Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Equivalent of %ld in Java for String.format()

For instance, %11.2lf in C++ becomes %11.2f in Java. How about for long format?

like image 725
Milli Avatar asked Jan 06 '10 08:01

Milli


People also ask

What is string format () in java?

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.

What is %d and %s in java?

%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"

What is %d for in java?

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.


2 Answers

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.

like image 109
Andrzej Doyle Avatar answered Oct 12 '22 15:10

Andrzej Doyle


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.

like image 32
Stu Thompson Avatar answered Oct 12 '22 13:10

Stu Thompson