problem with decimal in java

0

I have a product of two numbers

double resultado = XN1 * XN;

but that result would like to add a 0.resultado, I do with this method

String convertirdecimal(String cad) {
    String cad2 = "0." + cad;
    return cad2;
}

When that happened the chain that would be the result, but then when I returned the result that transformed it into a double , the problem comes when it is a result as 170 that adding the 0.170 to transform it into double remains 0.17 and I do not know why, I do not know if there would be a better way to add the 0. to the result

    
asked by Efrainrodc 22.05.2017 в 16:58
source

1 answer

1

You can achieve this by using DecimalFormat which is used to give the formats to numbers depending on your needs.

If you use it in the following way:

DecimalFormat formateador = new DecimalFormat("####.####");
// Imprime esto con cuatro decimales, es decir: 7,1234
System.out.println (formateador.format (7.12342383));

Crop the numbers as defined in the format. If you replace the # with 0 the numbers are filled with 0 the positions that you have defined in the format.

DecimalFormat formateador = new DecimalFormat("0000.0000");
// Imprime con 4 cifras enteras y 4 decimales: 0001,8200
System.out.println (formateador.format (1.82));
    
answered by 22.05.2017 / 17:07
source