<p>How can I format Floats in Java so that the float component is displayed only if it's not zero? For example:</p>

<pre>
123.45 -> 123.45
99.0   -> 99
23.2   -> 23.2
45.0   -> 45
</pre>

<p>Edit: I forgot to mention - I'm still on Java 1.4 - sorry!</p>

[{'answer_id': 54502, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 4, 'selected': True, 'text': '<p>If you use <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html" rel="nofollow noreferrer">DecimalFormat</a> and specify # in the pattern it only displays the value if it is not zero.</p>\n\n<p>See my question <a href="https://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java">How do I format a number in java?</a></p>\n\n<p>Sample Code</p>\n\n<pre><code> DecimalFormat format = new DecimalFormat("###.##");\n\n    double[] doubles = {123.45, 99.0, 23.2, 45.0};\n    for(int i=0;i&lt;doubles.length;i++){\n        System.out.println(format.format(doubles[i]));\n    }\n</code></pre>\n'}, {'answer_id': 54509, 'author': 'Andy Whitfield', 'author_id': 4805, 'author_profile': 'https://Stackoverflow.com/users/4805', 'pm_score': 2, 'selected': False, 'text': '<p>Check out the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html" rel="nofollow noreferrer">DecimalFormat</a> class, e.g. new DecimalFormat("0.##").format(99.0) will return "99".</p>\n'}, {'answer_id': 54511, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 0, 'selected': False, 'text': '<pre><code>new Formatter().format( "%f", myFloat )\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54487', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']