简体   繁体   中英

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). How can I specify in a DecimalFormat to display all numbers to the left rather than specifying the number of digits ahead of time?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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