Remove trailing zero in Java
java, regex, string
Solution
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
Problem
I have Strings (from DB), which may contain numeric values. If it contains numeric values, I'd like to remove trailing zeros such as: - `10.0000` - `10.234000` `str.replaceAll("\\.0*$", "")`, works on the first one, but not the second one. A lot of the answers point to use `BigDecimal`, but the `String` I get may not be numeric. So I think a better solution probably is through the Regex.