Well, the trick is that the floats and double themselves do not actually have trailing zeros; it's just how they are printed (or initialized as literals) that can show them. Consider the following examples:
Float.toString(10.5000); // => "10.5" Float.toString(10.0000); // => "10.0"
You can use DecimalFormat
to fix the example "10.0":
new java.text.DecimalFormat("#").format(10.0); // => "10"
source share