I want to format a string as a decimal, but the decimal contains some following zeros after the decimal. How do I format it such that those meaningless 0\'s disappear?
How about:
string FormatDecimal(decimal d)
{
const char point = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator[0];
string s = d.ToString();
// if there's no decimal point, there's nothing to trim
if (!s.Contains(point) == -1)
return s;
// trim any trailing 0s, followed by the decimal point if necessary
return s.TrimEnd('0').TrimEnd(point);
}