java fomat string "%,d" and "%, d"

4171 views java
2

Why is

public static void main(String arr[]){  
  System.out.println("[" + String.format("%, d",1000000000) + "]");
}

Writing it as [ 1,000,000,000], with a space in front of the number?

Also what does "%, d" mean as compared to "%,d" as a format specifier?

answered question

Added brackets to increase clarity - often useful in general print-'debugging' :}

2 Answers

11

"%, d" means that you are printing 1 space, then an integer with comma(s) ([ 1,000,000,000])

"%,d" means that you are printing an integer with comma(s) ([1,000,000,000])

"%d" means that you are printing an integer without comma(s) ([1000000000])

posted this
1

When you run following line

// extra space in front with number formatted
System.out.println(String.format("%, d",1000000000));  
// number formatted with ,
System.out.println(String.format("%,d",1000000000));
// just number
System.out.println(String.format("%d",1000000000));

OUTPUT:

 1,000,000,000
1,000,000,000
1000000000

posted this

Have an answer?

JD

Please login first before posting an answer.