0

Halo

can someone tell me how to convert a double variable to 2 decimals? I try using the below code but it cannot work.

DecimalFormat df = new DecimalFormat("0.00");

String a = df.format(123.5%11);

double AA = Double.parseDouble(a);

for instance i want AA = 2.50 instead of 2.5. the program keep on giving me 2.5.

thks

3
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by jwenting
0

Add a line df.setMinimumFractionDigits(2) before you format the results.
For good measure you may also want to add df.setMinimumIntegerDigits(1) to prevent 0.50 from being output as .50

0

I am facing a similar problem,i want to round off my bumbers till 2 digits after decimal points.e.g 4/6 to 0.66,3/5 to 0.80,etc
for variables prList and count,i tried
>>>round(len(prList)*1.0 / count,2)
This gives me long results like
0.67000000000000004
Even though i'm specifying the number 2.

0

Of course it does, the result is a floating point number which have arbitrary precision based on the size of the number.
The number of decimal places you provide is ONLY for purpose of internal precision of the calculation, if you want to present the number in that number of decimal places you will still need to use a formatting routine as already discussed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.