title: C# Convert object to Decimal
body: <p>I'm trying to convert an object with the value <code>0.39999999999999997</code> to a decimal variable without losing the precision. </p>
  
  <pre><code>object d = 0.39999999999999997;
  </code></pre>
  
  <p>I've tried the following methods.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I know the this is not a problem with the decimal data type not being able to store this value as illustrated below. </p>
  
  <pre><code>decimal val5 = 0.39999999999999997m; // val5 = 0.39999999999999997;
  </code></pre>
  
  <p>How do I convert this object to decimal without losing the precision?</p>
  
  <p>I'm using .NET Framework 3.5 if that matters.</p>