Search code examples
javastring-formatting

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?


Solution

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