简体   繁体   English

Java DecimalFormat包含左侧的所有数字

[英]Java DecimalFormat include all digits to the left

I'm trying to format an arbitrary length decimal so all of the numbers to the left of the decimal point are displayed but a maximum of 2 to the right are displayed (if they are non-zero). 我正在尝试格式化任意长度的十进制格式,以便显示小数点左侧的所有数字,但右侧最多显示2个数字(如果它们非零)。 How can I specify in a DecimalFormat to display all numbers to the left rather than specifying the number of digits ahead of time? 如何在DecimalFormat中指定显示所有数字到左侧,而不是提前指定位数?

thanks, Jeff 谢谢,杰夫

This might help you 这可能对您有帮助

DecimalFormat formatter = new DecimalFormat("#.##");

inputs/outputs: 输入/输出:

System.out.println(formatter.format(-1234.567));         
System.out.println(formatter.format(1239.00));                
System.out.println(formatter.format(567));
System.out.println(formatter.format(0.2342523));

gave

-1234.57
1239
567
0.23

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

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