title: Calculate percentages like Excel pie chart
body: <p>I'm translating a calculation model from Excel to a programming language, and I'd like to keep the results as similar to the Excel ones as possible.</p>
  
  <p>Specifically, I want to make sure pie chart percentages add up to 100 when they're displayed as whole numbers.</p>
  
  <p>Example: </p>
  
  <ul>
  <li>consider the set: <code>[ 7, 13, 58, 43 ]</code>. </li>
  <li>The percentages in a pie charts should be <code>[ 5.79%, 10.74%, 47.93%, 35.54% ]</code></li>
  <li>Round the numbers and you get <code>[ 6%, 11%, 48%, 36% ]</code>. These add up to 101.</li>
  <li>According to <a href="http://support.microsoft.com/kb/214033" rel="nofollow">this page</a>, Excel should subtract 1 percent from the largest number.</li>
  <li>That information doesn't apply to Excel 2007 apparently, since the actual numbers that I'm seeing are <code>[ 6%, 11%, 48%, 35% ]</code>, so it subtracted 1 from the second-largest number.</li>
  </ul>
  
  <p>I found <a href="http://peltiertech.com/WordPress/pie-chart-rounding-in-excel/" rel="nofollow">this page</a> that seems to acknowledge that the situation is weird, but doesn't really have the algorithm Excel uses. </p>
  
  <p><strong>EDIT</strong>: I missed it the first time, but that page implies that "the internal mechanism Excel uses minimizes the error in the percentages in a similar manner to the way a least squares minimizes the error between a data set and the regression line that describes it". Not sure if it's just a wild guess or something more.</p>
  
  <p>Can anyone shed a light on the matter?</p>
  
  <p>(Obligatory link: <a href="http://www.perceptualedge.com/articles/08-21-07.pdf" rel="nofollow">Save the Pie for Dessert</a>)</p>