Java格式化字符串"%,d"和"%,d"的含义。

3

Why is

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

将其写为[ 1,000,000,000 ],数字前面有一个空格?

另外,与格式说明符“%,d”相比,“%, d”的含义是什么?


1
添加括号以增加清晰度 - 在一般的打印“调试”中通常很有用 :} - user2864740
2个回答

10

"%, d" 意味着你将打印 1 个空格,然后是带逗号的整数([1,000,000,000]

"%,d" 意味着你将打印带逗号的整数([1,000,000,000]

"%d" 意味着你将打印不带逗号的整数([1000000000]


1

当您运行以下代码行时

// 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接