I'm trying to construct a single regex (for Java) to truncate trailing zeros past the decimal point. e.g.
- 50.000 → 50
- 50.500 → 50.5
- 50.0500 → 50.05
- -5 → -5
- 50 → 50
- 5.5 → 5.5
Idea is to represent the real number (or integer) in the most compact form possible.
Here's what I've constructed:
^(-?[.0-9]+?)\.?0+$
I'm using $1 to capture the truncated number string.
The problem with the pattern above is that 50 gets truncated to 5. I need some way to express that the 0+ must follow a . (decimal point).
I've tried using negative-behind, but couldn't get any matches.
^(-?[\.0-9]+?)(\.0+)?$