title: Entity framework decimal precision
body: <p>I'm using the Entity framework (6.1.3) (<strong><em>database</em></strong> first) for one of my projects where I have three tables in the database which contains decimal fields.</p>
  
  <p>The decimal fields are set with a precision of 18 and a scale of 5, like this: <code>decimal(18, 5)</code> </p>
  
  <p>I have checked the edmx XML file and it seems like the decimals are mapped as they should. I.e.:</p>
  
  <pre><code>&lt;Property Name="Price" Type="decimal" Precision="18" Scale="5" Nullable="false" /&gt;
  </code></pre>
  
  <p>However, when I try to save a calculated decimal value which looks like, i.e. <code>28021.800000000000000000000002</code>, I get an exception telling me that the property value is out of range.</p>
  
  <p>Shouldn't entity framework just save the precision and scaled value of this decimal? If not, how exactly do I make my decimal "valid"?</p>
  
  <p>Thanks in advance :-)</p>
  
  <p>Edited with code sample:</p>
  
  <pre><code>// Loop through billing price lines on this billing
  foreach (BillingPriceLine priceLine in billing.BillingPriceLines)
  {
        // Price line specification sum fields
        decimal totalPriceLineSpecificationProduction = 0;
        decimal totalPriceLineSpecificationSum = 0;
  
        // Loop through billing price line specifications on this price line
        foreach (BillingPriceLineSpecification specification in priceLine.BillingPriceLineSpecifications)
        {
              // First, check if the estimated production and realised production has a value
              if (specification.EstimatedProduction.HasValue &amp;&amp; specification.RealisedProduction.HasValue)
              {
                    // Calculate production for a price line specification
                    specification.Production = specification.EstimatedProduction.Value - specification.RealisedProduction.Value;
  
                    // Add production to total price line specification production
                    totalPriceLineSpecificationProduction = specification.Production;
  
                     // Add to total price line specification sum
                     totalPriceLineSpecificationSum += specification.Production * specification.Price;
                }
          }
  
          // Set total production on price line
          priceLine.Production = totalPriceLineSpecificationSum;
  
          // Set price on price line
          priceLine.Price = totalPriceLineSpecificationProduction / priceLine.Production;
  
           // Set total price on price line
           priceLine.TotalPrice = (priceLine.Production * priceLine.Price) * 100;
    }
  
    // Set subtotal, VAT and total sum on billing
    billing.Subtotal = billing.BillingPriceLines.Sum(x =&gt; x.TotalPrice);
    billing.VAT = billing.Subtotal / 4;
    billing.Total = billing.Subtotal + billing.VAT;
  
    // Save changes in database
    return ctx.SaveChanges() &gt; 0;
  </code></pre>