If you want 2 / 3 to result in 66.67, you need to multiply first, then divide and round to 2 decimal places:

BigDecimal totalPrep = BigDecimal.valueOf(2);
BigDecimal totalCase = BigDecimal.valueOf(3);

BigDecimal result = totalPrep.multiply(BigDecimal.valueOf(100))
                             .divide(totalCase, 2, RoundingMode.HALF_UP);

System.out.println(result); // prints: 66.67

That will calculate 2 * 100 = 200, then 200 / 3 = 66.67 (rounded)

When you divide first, you calculate 2 / 3 = 0.67 (rounded), then 0.67 * 100 = 67.00, which is obviously not what you want.

Answer from Andreas on Stack Overflow
🌐
Medium
medium.com › @AlexanderObregon › javas-bigdecimal-divide-method-explained-204688fe717a
Java’s BigDecimal.divide() Method Explained | Medium
January 13, 2025 - ... import java.math.BigDecimal; ... 30 years // Monthly payment before interest BigDecimal monthlyPayment = loanAmount.divide(numberOfPayments, 2, RoundingMode.HALF_EVEN); System.out.println("Monthly Payment: $" + ...
Discussions

java - BigDecimal divide with a large number of decimal places - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
java - Rounding BigDecimal to *always* have two decimal places - Stack Overflow
rounded 0.819 to 0.82 rounded 1.092 to 1.1 rounded 1.365 to 1.4 // should be 1.37 rounded 2.730 to 2.8 // should be 2.74 rounded 0.819 to 0.82 · I don't care about significant digits, I just want two decimal places. How do I do this with BigDecimal? More on stackoverflow.com
🌐 stackoverflow.com
round up to 2 decimal places in java? - Stack Overflow
import java.math.BigDecimal; public class RoundingNumbers { public static void main(String args[]){ double number = 123.13698; int decimalsToConsider = 2; BigDecimal bigDecimal = new BigDecimal(number); BigDecimal roundedWithScale = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("Rounded value with setting scale = "+roundedWithScale); bigDecimal = new BigDecimal(number); BigDecimal roundedValueWithDivideLogic = bigDecimal.divide... More on stackoverflow.com
🌐 stackoverflow.com
Java's Bigdecimal.divide and rounding - Stack Overflow
At work, we found a problem when trying to divide a large number by 1000. This number came from the database. Say I have this method: private static BigDecimal divideBy1000(BigDecimal dividendo) ... More on stackoverflow.com
🌐 stackoverflow.com
October 20, 2014
People also ask

How do I round a double to 2 decimal places in Java?
Use BigDecimal.valueOf(x).setScale(2, RoundingMode.HALF_UP), which turns 1.005 into 1.01 and pads 7 to 7.00. Call .doubleValue() on the result if you need a double back. The one-liner Math.round(x * 100.0) / 100.0 is fine for most values, but because 1.005 * 100.0 is 100.49999999999999, it returns 1.0 for 1.005.
🌐
compilebytes.com
compilebytes.com › home › examples › java examples › how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
What is the difference between setScale and round(MathContext) on a BigDecimal?
setScale counts decimal places and round(MathContext) counts significant digits. For 1234.5678, setScale(2, RoundingMode.HALF_UP) gives 1234.57, round(new MathContext(6)) gives the same 1234.57, but round(new MathContext(2)) gives 1.2E+3. To round to 2 decimal places, use setScale.
🌐
compilebytes.com
compilebytes.com › home › examples › java examples › how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
How do I fix "ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result"?
Give BigDecimal.divide a scale and a rounding mode. new BigDecimal("10.00").divide(new BigDecimal("3")) throws because 10 / 3 has no exact decimal form, while divide(divisor, 2, RoundingMode.HALF_UP) returns 3.33. To keep a number of significant digits instead of decimal places, pass a MathContext such as MathContext.DECIMAL64, which gives 3.333333333333333.
🌐
compilebytes.com
compilebytes.com › home › examples › java examples › how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
🌐
BeginnersBook
beginnersbook.com › 2024 › 05 › how-to-round-a-number-to-two-decimal-places-in-java
How to round a number to two decimal places in Java
May 31, 2024 - setScale(2, RoundingMode.HALF_UP) sets the scale of the BigDecimal to 2 decimal places.RoundingMode.HALF_UP is the rounding mode that rounds towards “nearest neighbour” unless both neighbours are at equal distance, in which case it rounds up.
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_divide_rdroundingmode_scale.htm
Java.math.BigDecimal.divide() Method
The java.math.BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode) returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java basics › round a double to 2 decimal places in java (or n places)
Round a Double to 2 Decimal Places in Java (or N Places)
February 16, 2024 - The quickest way without BigDecimal is to multiply by 100, round to a whole number with Math.round() and divide by 100.0 again. For N places, we multiply and divide by 10 to the power of N. The division must use 100.0 and not 100, otherwise the long result is divided as an integer and we lose the decimals...
🌐
CompileBytes
compilebytes.com › home › examples › java examples › how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
First, divide(other) with no scale must return the exact quotient, and 10 / 3 has no exact decimal form, so it throws. Give it a scale and a rounding mode, which also rounds the result to two decimals in one step.
🌐
John Dalesandro
johndalesandro.com › home › blog › rounding decimals in java: avoiding round-off errors with bigdecimal
Rounding Decimals in Java: Avoiding Round-Off Errors With BigDecimal
March 21, 2025 - Method 4: Rounded using BigDecimal Input value: 3.14159265358979323846264338327950288419716939937510 -------- Rounded to 0 decimal places: 3 Rounded to 1 decimal places: 3.1 Rounded to 2 decimal places: 3.14 Rounded to 3 decimal places: 3.142 Rounded to 4 decimal places: 3.1416 Rounded to 5 decimal places: 3.14159 Rounded to 6 decimal places: 3.141593 Rounded to 7 decimal places: 3.1415927 Rounded to 8 decimal places: 3.14159265 Rounded to 9 decimal places: 3.141592654 Rounded to 10 decimal places: 3.1415926536 Rounded to 11 decimal places: 3.14159265359 Rounded to 12 decimal places: 3.1415926
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-divide-method-in-java-with-examples
BigDecimal divide() Method in Java with Examples - GeeksforGeeks
July 12, 2025 - public BigDecimal divide(BigDecimal divisor, MathContext mc) Parameters: This method accepts two parameters: ... mc of type MathContext for context settings. Return value: This method returns a BigDecimal which holds the result (this / divisor) ...
Top answer
1 of 7
26

As specified in javadoc, a BigDecimal is defined by an integer value and a scale.

The value of the number represented by the BigDecimal is therefore (unscaledValue × 10^(-scale)).

So BigDecimal("1761e+5") has scale -5 and BigDecimal(176100000) has scale 0.

The division of the two BigDecimal is done using the -5 and 0 scales respectively because the scales are not specified when dividing. The divide documentation explains why the results are different.

divide

public BigDecimal divide(BigDecimal divisor)

Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

Parameters:

divisor - value by which this BigDecimal is to be divided.

Returns:

this / divisor

Throws:

ArithmeticException — if the exact quotient does not have a terminating decimal expansion

Since:

1.5

If you specify a scale when dividing, e.g. dividendo.divide(BigDecimal.valueOf(1000), 0, RoundingMode.HALF_UP) you will get the same result.

2 of 7
7

The expressions new BigDecimal("176100000") and new BigDecimal("1761e+5") are not equal. BigDecimal keeps track of both value, and precision.

BigDecimal("176100000") has 9 digits of precision and is represented internally as the BigInteger("176100000"), multiplied by 1. BigDecimal("1761e+5") has 4 digits of precision and is represented internally as the BigInteger("1761"), multiplied by 100000.

When you a divide a BigDecimal by a value, the result respects the digits of precision, resulting in different outputs for seemingly equal values.

🌐
javaspring
javaspring.net › blog › how-to-round-to-2-decimal-places-in-java
How to Round to 2 Decimal Places in Java — javaspring.net
Math.round is a static method in Java that rounds a floating-point number to the nearest integer. You can use it to round a number to 2 decimal places by multiplying the number by 100, rounding it to the nearest integer, and then dividing it by 100.
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Java: How to Round a Number to N Decimal Places | Sentry
September 11, 2026 - You can specify both the number of decimal places and the rounding mode. For example, the following code creates a BigDecimal object from the double value and uses the setScale() method to specify two decimal places and the HALF_UP rounding mode:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - The individual multiplications ... of decimal digits in n. if n is negative, the result is calculated as if n were positive; this value is then divided into one using the working precision specified above. The final value from either the positive or negative case is then rounded to the destination precision. ... ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY, or n is out of range. ... Returns a BigDecimal whose value ...
🌐
Stack Overflow
stackoverflow.com › questions › 67556279 › unable-to-perform-divide-operation-in-bigdecimal-java-without-rounding-off
Unable to perform divide operation in BigDecimal Java without rounding off - Stack Overflow
Because you have set it to 2, it will round it down to 2 decimals (hence you get 0.05). You could therefore just increase the scale of your division operation: ... Sign up to request clarification or add additional context in comments.
🌐
Java67
java67.com › 2020 › 04 › 4-examples-to-round-floating-point-numbers-in-java.html
4 Examples to Round Floating-Point Numbers in Java up to 2 Decimal Places | Java67
Rounding numbers up to 2 or 3 decimal places id a common requirement for Java programmers. Thankfully, Java API provides a couple of ways to round numbers in Java. You can round any floating-point numbers in Java unto n places By using either Math.round(), BigDecimal, or DecimalFormat.