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 fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 59,467 times.
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] X Research source
Steps
-
Java, like other programming languages, uses integer division by default. 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] X Research source is performed. In this example, we'll cast a todouble
so we get a decimal result:int a = 55 ; int b = 25 ; double r = ( double ) a / b // The answer is 2.2.
-
When you're dividing a decimal number by another decimal number, you'll use double division. [3] X Research source 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
-
If you're working with currency or need the most precise decimal result, use the BigDecimal class. Floating point arithmetic (which is what you're doing with
double
) is less precise, asdouble
stores numbers as binary representations of fractions and exponents instead of exact representations (fixed-point numbers). [4] X Research source To ensure you're working with fixed-point numbers, useBigDecimal
. 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 beUP
(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), orUNNECESSARY
(result should be exact). [5] X Research source
- When you use BigDecimal, you'll need to specify the
Expert Q&A
Tips
You Might Also Like
References
- ↑ https://www.geeksforgeeks.org/bigdecimal-class-java/
- ↑ https://mathbits.com/MathBits/Java/DataBasics/Mathoperators.htm
- ↑ https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- ↑ https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
- ↑ https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html