DecimalFormat does not show me 0 in decimals

0

I have this:

public String refrescarPrecio(double cantidad)
{
    DecimalFormat df = (DecimalFormat)NumberFormat.getInstance();
    df.applyPattern("$ ###,###.##");

    String str = "";
    str = df.format(cantidad);

    return str;
}

If the calculation that my program does should show "0.30", it only shows "0.3".

How could I fix it? I tried replacing the "#" with "0" but it does not work.

    
asked by Robert Gomez 16.11.2016 в 02:46
source

3 answers

3

If I change "$ ###,###.##" by "$ ###,##0.00" , it works very well for me.

public String refrescarPrecio(double cantidad)
{
    DecimalFormat df = (DecimalFormat)NumberFormat.getInstance();
    df.applyPattern("$ ###,##0.00");

    return df.format(cantidad);
}

Demo .

    
answered by 16.11.2016 / 05:09
source
0

This could help you

double n = 651.5176515121351;

n = Math.round(n * 100);
n = n/100;

Edit 1:

cantidad = roundTwoDecimals(cantidad);

public double roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
}
    
answered by 16.11.2016 в 04:24
-1

How are you printing? if you only want to print the result you can use the following:

 System.out.printf("%.2f",variable); #Imprime con 2 decimales

Hopefully this link will serve you: link

    
answered by 16.11.2016 в 03:50