# Remove Trailing Zeros in decimal value

I am now working on an application where there are lot of calculations involved. The calculated value is a decimal which may or may not have fraction digits. Also, the total no. of fraction digits is unknown.

Now the problem is the decimal value had lot of trailing zeros. When I display this in UI, I need to display without the trailing zeros. Initially, I used **{0:n5}** as format string. But when the decimal value was something like **10.67895489000**, it was rounded to 5 fraction digits.

Now this application is used in healthcare industry & even a small fraction could make big difference for them. So I am not allowed to do any rounding of values.

The big question is how to fix this issue.. I tried several approaches with formatting. But nothing was that much useful. Finally decided to truncate zeros with a simple method. So, here is the solution. *This solution holds good even if you are using different cultures.*

private string RemoveTrailingZeros(decimal input) { string output = string.Empty; decimal number = 10; if (input == decimal.Truncate(input)) // no decimal places, is a whole number { output = input.ToString("N0"); } else { for (int i = 1; i < 100; i++) { if (number > 0) //Multiply by 0 will result in incorrect output. This can happen if decimal.maxValue is reached by number. { if (input * number == decimal.Truncate(input * number)) // dealing with a one decimal place value { string format = "N" + i; output = input.ToString(format); break; } number *= 10; } } } return output; }

Experience bring great knowledge. The code is really good. Great work Uma. Thanks