PDF download Download Article PDF download Download Article

This wikiHow article will show you three ways to do decimal numbers in Java. If you want to divide two integers (non-decimal) and get a non-rounded answer, cast one of the operands to a double. To divide two decimal numbers (or an integer by a decimal or vice-versa), you can use simple double division to ensure a decimal result. When working with larger numbers or numbers that needs to be extremely precise, you can use the BigDecimal Java class instead of floating point arithmetic. [1]

1

Get a decimal result from two integers

PDF download Download Article
  1. If dividing two integers results in a remainder, the remainder is discarded, leaving you with a whole integer as the answer. If you need to divide two integers and get a decimal result, you can cast either the numerator or denominator to double before the operation. [2] is performed. In this example, we'll cast a to double so we get a decimal result:
     int 
     a 
     = 
     55 
     ; 
     int 
     b 
     = 
     25 
     ; 
     double 
     r 
     = 
     ( 
     double 
     ) 
     a 
     / 
     b 
     // The answer is 2.2. 
    
  2. Advertisement
2

Divide two decimal numbers (doubles)

PDF download Download Article
  1. [3] Similarly, if one of the two operands is an integer (a non-decimal number), the result will still be a decimal number if the other operand is a double. Here is a simple example of dividing two decimal numbers with double division:
     double 
     x 
     = 
     10.5 
     ; 
     double 
     y 
     = 
     2.5 
     ; 
     x 
     / 
     y 
     // the answer is 4.2 
    
3

Get the most precise decimal results with BigDecimal

PDF download Download Article
  1. Floating point arithmetic (which is what you're doing with double ) is less precise, as double stores numbers as binary representations of fractions and exponents instead of exact representations (fixed-point numbers). [4] To ensure you're working with fixed-point numbers, use BigDecimal . In this example, we'll use BigDecimal to divide two numbers for a precise result:
     BigDecimal 
     bdec 
     = 
     new 
     BigDecimal 
     ( 
     "706" 
     ); 
     BigDecimal 
     bdecRes 
     = 
     bdec 
     . 
     divide 
     ( 
     new 
     BigDecimal 
     ( 
     "20" 
     )); 
     System 
     . 
     out 
     . 
     println 
     ( 
     "Divide: " 
     + 
     bdecRes 
     ); 
     // Divide with MathContext 
     MathContext 
     mc 
     = 
     new 
     MathContext 
     ( 
     2 
     , 
     RoundingMode 
     . 
     FLOOR 
     ); 
     BigDecimal 
     bdecMath 
     = 
     bdec 
     . 
     divide 
     ( 
     new 
     BigDecimal 
     ( 
     "20" 
     ), 
     mc 
     ); 
     System 
     . 
     out 
     . 
     println 
     ( 
     "Divide with MathContext: " 
     + 
     bdecMath 
     ); 
     // the first result will be 45.25, and the second will be 45. 
    
    • When you use BigDecimal, you'll need to specify the RoundingMode for the result, which can be UP (away from zero), DOWN (toward zero), CEILING (toward positive infinity), FLOOR (toward negative infinity), HALF_UP (toward nearest neighbor, or up if both neighbors are equal), HALF_DOWN (toward nearest neighbor, or down if equal), HALF_EVEN (toward nearest neighbor, or to the nearest even neighbor if equal), or UNNECESSARY (result should be exact). [5]
  2. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      Thanks to all authors for creating a page that has been read 59,467 times.

      Did this article help you?

      Advertisement