DecimalFormat « decimal « Java Data Type Q&A





1. How to always show decimal part when using DecimalFormat?    stackoverflow.com

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 ...

2. DecimalFormat drops the 0s after decimal point. I need to keep them.    coderanch.com

Hi, I have to show the number as is without dropping the end 0s after decimal. e.g. I need to show 12 as 12, 13.0 as 13.0 14.50 as 14.50. But the decimal format removes the last zero. Unfortunately, JFreeChart tooltip logic requires a decimal format to be specified to do anything like this. Any idea how we can achieve this. ...