Pregunta

I am trying to set decimal values,Below is my input string

String rate="1.000000000";

Converting to double:

Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8

System.out.println("ouput"+format.format(rate)); //Giving output as 1.

I dont understand how to do this,Any hints please.

Regards,

Chaitu

¿Fue útil?

Solución

Try

DecimalFormat format=new DecimalFormat("#.00000000");

and

System.out.println("ouput"+format.format(converted));

Otros consejos

# will not be displayed for 0, use 0 instead:

    String rate="1.010000000";
    Double converted=Double.valueOf(rate);
    DecimalFormat format=new DecimalFormat("0.00000000");       
    System.out.println("ouput "+format.format(converted));

Firstly, you're passing the string rate to the DecimalFormat.format method. This will fail, you need to pass the converted object in.

When I tested your code with the above changes, I got 1.01 in the output. To format to 8 decimal places, follow Bala Rs comments. i.e. DecimalFormat format = new DecimalFormat("#.00000000");

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top