Xorty Xorty - 1 year ago 102
Java Question

How to always show decimal part when using DecimalFormat?

Float sum = new Float(300); // always somehow calculated
DecimalFormat df = new DecimalFormat("#.#");
String s = df.format(sum/3); // prints 100, I want 100.0
s = df.format(301/3); // pritns 100.3 which is correct

Result should always be formatted to 1 decimal palce, how to do so?

Answer Source


DecimalFormat df = new DecimalFormat("#.#");


DecimalFormat df = new DecimalFormat("#.0");

Basically, a 0 means "always show the digit in this position", where a # means "show the digit in this position unless it's zero".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download