title: Missing last value while counting number of data in each bin of histogram using java
body: <p>Using below sample data :</p>
  <pre><code>List&lt;Double&gt; sampleData = Arrays.asList(1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.);
  </code></pre>
  <p>We know the <code>binWidth</code> or <code>binSize</code> is 2 using the formula:</p>
  <pre><code>double binWidth = (max-min) / numberOfBins;
  </code></pre>
  <p>Where the <code>numberOfBins</code> are 5 using the Sturge’s formula:</p>
  <pre><code>int numbetOfBins = (int)Math.ceil((1.0 + (3.3 * (Math.log10(exampleData.size())))));
  </code></pre>
  <p>Now, I am trying to count the number of data in each bin of histogram. Using below code:</p>
  <pre><code>public void calculateBinsNumberOfData() {
      double binWidth = (max-min) / numberOfBins;
      System.out.println(&quot;Bin Width is: &quot; + binWidth);
      double binMin = min, binMax, binDataPoints;
      for (int i = 1; i &lt;= numberOfBins; i++) {
          binMax = binMin + binWidth;
          binDataPoints = 0;
          for (Double double1 : sampleData) {
              if(double1 &gt;= binMin &amp;&amp; double1 &lt;= binMax) {
                  binDataPoints++;
              }
          }
          System.out.println(&quot;Bin &quot;+i+&quot; : [&quot;+binMin+&quot; to &quot;+binMax+&quot;] = &quot; + binDataPoints);
          binMin = binMax;
      }
  }
  </code></pre>
  <p>The output prints the results when including the last edge of a bin <code>double1 &lt;= binMax</code> like:</p>
  <p><a href="https://i.stack.imgur.com/QE7r6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QE7r6.png" alt="Number of data in each bin" /></a></p>
  <p>The output prints the results when not including the last edge of a bin <code>double1 &lt; binMax</code> like:</p>
  <p><a href="https://i.stack.imgur.com/FUjem.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FUjem.png" alt="enter image description here" /></a></p>
  <p>And, you can observe in both above cases we are missing the value <code>11</code> of sample data. Now, I don't know how to fix this problem in this special case so that we can effectively count the last value of the data.</p>