简体   繁体   English

如何在Java中使用DecimalFormat格式化整数?

[英]How to format integers using DecimalFormat in Java?

How does the syntax of DecimaFormat work? DecimaFormat的语法如何工作? I'm trying to make 1234567890 --> 1-234-56789-0 . 我正在尝试制作1234567890 > 1-234-56789-0 So I tried DecimalFormat("#-###-#####-#); But that's clearly wrong! 所以我尝试了DecimalFormat("#-###-#####-#);但这显然是错误的!

I was so hopeful that my original answer would solve your problem, but.. no. 我非常希望我的原始答案能够解决您的问题,但是..没有。 See this extract from DecimalFormat 's javadocs. 请参见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. 分组大小是分组字符之间的数字位数,例如100,000,000为3或1,0000,0000为4。 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 "#,##,###,####" == "######,####" == "##,####,####". 因此,“#,##,###,####” ==“ ######,####” ==“ ##,####,####”。


I had originally suggested using DecimalFormatSymbols . 我最初建议使用DecimalFormatSymbols This will allow you to change the comma to a minus, but it won't allow for variable-length groups. 这将使您可以将逗号更改为减号,但不允许使用可变长度的组。 So your requirements cannot be met using the built-in support for grouping. 因此,使用内置的分组支持无法满足您的要求。 You need to build your own formatter or do it manually. 您需要构建自己的格式化程序或手动进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM