How can I format Floats in Java so that the float component is displayed only if it’s not zero? For example:
123.45 -> 123.45 99.0 -> 99 23.2 -> 23.2 45.0 -> 45
Edit: I forgot to mention – I’m still on Java 1.4 – sorry!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you use DecimalFormat and specify # in the pattern it only displays the value if it is not zero.
See my question How do I format a number in java?
Sample Code