title: Compute weighted averages for large numbers
body: <p>I'm trying to get the weighted average of a few numbers. Basically I have:</p>
  
  <pre><code>Price    - 134.42
  Quantity - 15236545
  </code></pre>
  
  <p>There can be as few as one or two or as many as fifty or sixty pairs of prices and quantities.  I need to figure out the weighted average of the price. Basically, the weighted average should give very little weight to pairs like</p>
  
  <pre><code>Price    - 100000000.00
  Quantity - 3
  </code></pre>
  
  <p>and more to the pair above.  </p>
  
  <p>The formula I currently have is:</p>
  
  <pre><code>((price)(quantity) + (price)(quantity) + ...)/totalQuantity
  </code></pre>
  
  <p>So far I have this done:</p>
  
  <pre><code>        double optimalPrice = 0;
          int totalQuantity = 0;
          double rolling = 0;
          System.out.println(rolling);
  
          Iterator it = orders.entrySet().iterator();
          while(it.hasNext()) {
              System.out.println("inside");
              Map.Entry order = (Map.Entry)it.next();
              double price = (Double)order.getKey();
              int quantity = (Integer)order.getValue();
              System.out.println(price + " " + quantity);
  
              rolling += price * quantity;
              totalQuantity += quantity;
              System.out.println(rolling);
          }
          System.out.println(rolling);
          return rolling/totalQuantity;
  </code></pre>
  
  <p>The problem is I very quickly max out the "rolling" variable.</p>
  
  <p>How can I actually get my weighted average?</p>