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.


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

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
Even though i'm specifying the number 2.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.