I'd like to change float like this way:
10.5000 -> 10.5 10.0000 -> 10
How can I delete all zeros after the decimal point, and change it either float (if there's non-zeros) or int (if there were only zeros)?
Thanks in advance.
Well the trick is that floats and doubles themselves don't really have trailing zeros per se; it's just the way they are printed (or initialized as literals) that might show them. Consider these examples:
Float.toString(10.5000); // => "10.5"
Float.toString(10.0000); // => "10.0"
You can use a DecimalFormat
to fix the example of "10.0":
new java.text.DecimalFormat("#").format(10.0); // => "10"