Remove trailing zero in Java

there are possibilities:

1000    -> 1000
10.000  -> 10 (without point in result)
10.0100 -> 10.01 
10.1234 -> 10.1234

I am lazy and stupid, just

s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");

Same solution using contains instead of indexOf as mentioned in some of the comments for easy understanding

 s = s.contains(".") ? s.replaceAll("0*$","").replaceAll("\\.$","") : s

Leave a Comment