Regex for removing trailing zeros

.net, regex

Solution

If the numbers are embedded in a random string, regexes are the way to go:

Replace `(?<=\.\d+?)0+(?=\D|$)` with an empty string

That will trim trailing zeros if they appear after the period in a decimal number (it will always leave a single zero). It also accounts for numbers that appear at the very end.

And since you've added the .NET tag in the meantime, here's the code:

string replaced = Regex.Replace(
                         inputString,
                         @"(?<=\.\d+?)0+(?=\D|$)",
                         String.Empty);

Problem

I am looking for a regular expression (.NET) to remove trailing zeros: ``` 11645766.560000001000 -> 11645766.560000001 10190045.740000000000 -> 10190045.74 1455720.820000000100 -> 1455720.8200000001 ``` etc... I am using regex, over String.Trim(), because the numbers are in one string, actual example: ``` !BEGIN !>>C85.18 POS_LEVEL.T129{11645766.560000001000} = POS_LEVEL.T129 {10190045.740000000000} + WORK_LEVEL.T129{1455720.820000000100} END; ``` need to convert to: ``` !BEGIN !>>C85.18 POS_LEVEL.T129{11645766.560000001} = POS_LEVEL.T129{10190045.74} + WORK_LEVEL.T129{1455720.8200000001} END; ```

Original source