Search code examples
javafloating-pointzero

in Java, how to delete all 0s in float?


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.


Solution

  • Why not try regexp?

    new Float(10.25000f).toString().replaceAll("\\.?0*$", "")