Display only 2 digits after decimal

bill[p][l][0] = new DecimalFormat("##.##").format(Double.parseDouble(i2[m][0]));

      

The above code doesn't work; input 10.0

gives 10

as output.

+3


source to share


3 answers


It works - means that the digit is printed only if it is relevant (see documentation ). Try to use



bill[p][l][0] = new DecimalFormat("##.00").format(Double.parseDouble(i2[m][0]));

      

+5


source


To format to exactly two decimal places, you must use ##.00

as ##.##

will remove trailing zeros from the value.



+3


source


If you want to handle both positive and negative numbers, I would recommend using BigDecimal

as it Double

technically has no decimal values ​​to truncate. Try something like this:

new BigDecimal(i2[m][0]).setScale(2, BigDecimal.ROUND_FLOOR)

      

0


source







All Articles