I find all the other solution too complicated. Just
s.replaceFirst("\\.0*$|(\\.\\d*?)0+$", "$1");
performs this work. First he tries the first option, so the point followed by all zeros is replaced with nothing (since the group does not receive the set). Otherwise, if he finds a point followed by some digits (as small as possible due to the lazy quantifier *? ), Followed by some zeros, the zeros will be discarded because they are not included in the group. He is working .
A warning
My code relies on my assumption that adding an unrivaled group does nothing . This is true for an Oracle implementation, but not for others, including Android , that seem to add the string "null". I would call such implementations broken, as that just doesn't make sense, but they are correct according to Javadoc .
maaartinus
source share