I have tried to format(Truncate) a decimal value to 4 decimal places. For example, I want to convert decimal numbers like
31.818181818181818181818181818M or 31.818181818181818181M or 31.81818M
to
31.8181
(NOT rounding to 31.8182) And store it in to a nullable decimal variable. I have tried following decimal formatting without rounding .net and Stop Rounding In C# At A Certain Number but no luck for nullable decimals.
Here is the code
private decimal? ReturnNullableDecimal(decimal? initValue)
{
//e.g. initValue = 35M;
initValue = 35M; //just to debug;
decimal? outputValue = null;
if (initValue != null)
outputValue = initValue / (decimal)1.10;
//now outputValue is 31.818181818181818181818181818M
outputValue = Convert.ToDecimal(string.Format("{0:0.0000}", outputValue)); // <- this should be 31.8181 but gives 31.8182
return outputValue;
}
Can someone please help?