title: How to multiply LONG and FLOAT in Java
body: <p>I need to multiply two numbers (for example, <strong>A</strong> and <strong>B</strong>) and return the result (<strong>A*B</strong>).</p>
  <p><strong>Condition:</strong></p>
  <p><strong>number A</strong>: Any number between <strong>0</strong> to <strong>Long.MAX_VALUE</strong> ( i.e., 0 &lt;= A &lt;= 9,223,372,036,854,775,807)</p>
  <p><strong>number B</strong>: Any number between <strong>0</strong> to <strong>10000.0000000000</strong>) // Remember 'B' will be a decimal number with upto 10 decimal places</p>
  <p>If the result of (<strong>A * B</strong>) contains decimal, then, round up to the next higher integer.
  for example,</p>
  <ol>
  <li>if the result of A x B = 10.1 =&gt; 11</li>
  <li>if the result of A x B = 10.5 =&gt; 11</li>
  <li>if the result of A x B = 10.9 =&gt; 11</li>
  <li>if the result of A x B &gt;= LONG_MAX, then return LONG_MAX (i.e. 9223372036854775807)</li>
  </ol>
  <p>I wrote a simple function as mentioned but it is giving me strange result!! Can you please correct me why the function is failing?</p>
  <pre><code>static long multiply(long A, float B)
  {
      long result = (long) Math.ceil(A * B);
      return result;
  }
  
  public static void main(String args[])
  {
      System.out.println(multiply(10L, 1.11F)); // 10 x 1.11 =&gt; 11.1 =&gt; 12 [PASS]
      
      System.out.println(multiply(10L, 1.17F)); //10 x 1.17 =&gt; 11.7 =&gt; 12 [PASS]
      
      System.out.println(multiply(Long.MAX_VALUE, 1.11F)); // 9223372036854775807 x 1.11 =&gt; 9223372036854775807 [PASS]
      
      System.out.println(multiply(9999999L, 9.9999F)); // 9999999 x 9.9999 =&gt; 99,998,990.0001 =&gt; 99,998,991 [FAIL]
                                                       // I am getting 99998992, instead of 99998991 !!
      
      System.out.println(multiply(4611686018427387908L, 1.0F)); // 4611686018427387908L x 1 =&gt; 4611686018427387908 [FAIL]
                                                                // I am getting 4611686018427387904 instead of 4611686018427387908
      
      System.out.println(multiply(96113850L, 1.0F)); // 96113850 x 1 =&gt; 96113850 [FAIL]
                                                     // I am getting 96113848 instead of 96113850
  }
  </code></pre>
  <p>Last 3 test cases are failing. Please let me know how to correct it?</p>
  <p>Thank you.</p>