I use DecimalFormat to create a formatted decimal that always contains 6 characters. At first I used the format string new DecimalFormat("000.00") , but this gave me an error for negative numbers. A minus sign is added and makes space number one large, resulting in -005.25 , rather than -05.25 as desired.
I was able to fix this with the following code
DecimalFormat fmt; if(netAmt < 0){ fmt = new DecimalFormat("00.00"); }else{ fmt = new DecimalFormat("000.00"); } System.out.println(fmt.format(netAmt));
But DecimalFormat has a character ; to format negative numbers differently than positive numbers. I could not do this job correctly. As far as I understand, the following code should work the same as above.
DecimalFormat fmt = new DecimalFormat("000.00;00.00"); System.out.println(fmt.format(netAmt));
The result is that the pattern is before ; It is used for both negative and positive numbers that cause the error -005.25 . What am I doing wrong? I do not understand why ; ?
java decimalformat
Ama daden
source share