Download Article 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

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)

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

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
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

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

About This Article

Anne Schmidt
Written by:
Chemistry Instructor
This article was written by Anne Schmidt and by wikiHow staff writer, Nicole Levine, MFA. Anne Schmidt is a Chemistry Instructor in Wisconsin. Anne has been teaching high school chemistry for over 20 years and is passionate about providing accessible and educational chemistry content. She has over 9,000 subscribers to her educational chemistry YouTube channel. She has presented at the American Association of Chemistry Teachers (AATC) and was an Adjunct General Chemistry Instructor at Northeast Wisconsin Technical College. Anne was published in the Journal of Chemical Education as a Co-Author, has an article in ChemEdX, and has presented twice and was published with the AACT. Anne has a BS in Chemistry from the University of Wisconsin, Oshkosh, and an MA in Secondary Education and Teaching from Viterbo University. This article has been viewed 57,452 times.
8 votes - 100%
Co-authors: 3
Updated: March 3, 2024
Views: 57,452
Categories: Mathematics
Thanks to all authors for creating a page that has been read 57,452 times.

Did this article help you?

Advertisement