1

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

4

3 に答える 3

5

Try

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

and

System.out.println("ouput"+format.format(converted));
于 2011-05-24T12:42:49.800 に答える
4

# 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));
于 2011-05-24T12:45:17.440 に答える
1

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");

于 2011-05-24T12:49:20.437 に答える