Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java format string "%,d" and "%, d"

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?

like image 434
rimalroshan Avatar asked Sep 15 '25 16:09

rimalroshan


2 Answers

"%, 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])

like image 54
Andreas Avatar answered Sep 18 '25 04:09

Andreas


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
like image 31
MyTwoCents Avatar answered Sep 18 '25 06:09

MyTwoCents