How do I format a C# decimal to remove extra following 0’s?

You can use ToString() with the General (“G”) Format Specifier to achieve the desired result. Trailing zeros are truncated when using this format string with a precision specified. In order to prevent rounding in any situations, you will need to set the precision to the maximum allowed for decimals (29).

The line of code to produce what you want is number.ToString("G29"), where number is your original decimal.

Be aware that any numbers smaller than 0.0001 will be converted to scientific notation. More details on how this formatter works can be found at the reference link above.

Leave a Comment