I have a decimal in in my program which could possibly have trailing zeros after a decimal point and I would like to get rid of them.
I wish to do this by converting the decimal to a string and only using a sub-string based on where a decimal point is found and if the content after the decimal point is just zeros. This is what I came up with:
string number = Convert.ToString(MyDouble);
if (number.Contains("."))//If there are trailing zeros
{
//If the numbers after the decimal point (when taken as an integer) equal 0
if(Convert.ToInt16(number.Substring(number.IndexOf("."))) == 0)
{
//Remove anything after the decimal point
number = number.Substring(0, number.Length - number.IndexOf("."));
}
}
result = Convert.ToDecimal(number);
But this doesn't work. What's wrong with it?