DecimaFormat
的语法如何工作?
我正在尝试制作 1234567890
--> 1-234-56789-0
。所以我尝试了 DecimalFormat("#-###-#####-#);
但这显然是错误的!
最佳答案
我非常希望我原来的答案能够解决您的问题,但是.. 没有。请参阅 DecimalFormat
的 javadocs 中的摘录。
<小时/>The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
我最初建议使用DecimalFormatSymbols
。这将允许您将逗号更改为减号,但不允许使用可变长度组。因此,使用内置的分组支持无法满足您的要求。您需要构建自己的格式化程序或手动执行。
关于java - 如何在 Java 中使用 DecimalFormat 格式化整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27413404/