Reputation: 1463
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
Upvotes: 1
Views: 253
Reputation: 888
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");
Upvotes: 1
Reputation: 137442
#
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));
Upvotes: 4
Reputation: 109027
Try
DecimalFormat format=new DecimalFormat("#.00000000");
and
System.out.println("ouput"+format.format(converted));
Upvotes: 5