I'm trying to convert an object with the value 0.39999999999999997
to a decimal variable without losing the precision.
object d = 0.39999999999999997;
I've tried the following methods.
decimal val1 = Convert.ToDecimal(d); // val1 = 0.4
object val2 = Convert.ChangeType(d, Type.GetType("System.Decimal")); // val2 = 0.4
decimal val3 = decimal.Parse(d.ToString()); // val3 = 0.4
decimal val4 = (Decimal) d; // val4 = 0.4
I know the this is not a problem with the decimal data type not being able to store this value as illustrated below.
decimal val5 = 0.39999999999999997m; // val5 = 0.39999999999999997;
How do I convert this object to decimal without losing the precision?
I'm using .NET Framework 3.5 if that matters.