You haven't specified a scale for the result. Please try this

2019 Edit: Updated answer for JDK 13. Cause hopefully you've migrated off of JDK 1.5 by now.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("1");
        BigDecimal b = new BigDecimal("3");
        BigDecimal c = a.divide(b, 2, RoundingMode.HALF_UP);
        System.out.println(a + "/" + b + " = " + c);
    }

}

Please read JDK 13 documentation.

Old answer for JDK 1.5 :

import java.math.*; 

    public class x
    {
      public static void main(String[] args)
      {
        BigDecimal a = new BigDecimal("1");
        BigDecimal b = new BigDecimal("3");
        BigDecimal c = a.divide(b,2, BigDecimal.ROUND_HALF_UP);
        System.out.println(a+"/"+b+" = "+c);
      }
    }

this will give the result as 0.33. Please read the API

Answer from Rohan Grover on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal divide with rounding modes and scale
BigDecimal Division in Java: Rounding Modes & Scale
September 1, 2008 - 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. If rounding must be performed to generate a result with the specified ...
🌐
Medium
medium.com › @AlexanderObregon › javas-bigdecimal-divide-method-explained-204688fe717a
Java’s BigDecimal.divide() Method Explained | Medium
January 13, 2025 - Division with Scale and Rounding Mode: For more control, the divide() method allows specifying both the scale (number of decimal places) and a rounding mode. This is particularly useful in applications where the precision of the result must ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-divide-method-in-java-with-examples
BigDecimal divide() Method in Java with Examples - GeeksforGeeks
July 12, 2025 - There are five overloads of divide method available in Java which is listed below: ... divide(BigDecimal divisor, int roundingMode) Note: The method divide(BigDecimal divisor, int roundingMode) is deprecated since Java 9.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. The new divide(BigDecimal, int, RoundingMode) method should be used in preference to this ...
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_divide_rdroundingmode.htm
Java.math.BigDecimal.divide() Method
Following is the declaration for ... BigDecimal is to be divided. roundingMode − Rounding mode to apply. This method returns a BigDecimal object whose value is this / divisor, rounded as necessary....
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.divide
BigDecimal.Divide Method (Java.Math) | Microsoft Learn
Portions of this page are modifications ... Creative Commons 2.5 Attribution License. Returns a BigDecimal whose value is (this / divisor), with rounding according to the context settings....
🌐
Coderanch
coderanch.com › t › 450954 › java › avoid-rounding-BigDecimal-division
How to avoid rounding while BigDecimal division (Java in General forum at Coderanch)
So you must round somewhere for BigDecimal for any number not a multiple of 2 and 5 only. 2048 is a multiple of 2 only, but 2047 isn't. so you must round. Choose a larger precision for your rounding. ... Campbell Ritchie wrote:Unless you divide by a number whose prime factors only include 2 and 5, every division will recur, and go on to infinity.
🌐
GitHub
github.com › tc39 › proposal-decimal › issues › 13
BigDecimal with division and other operations which need to round · Issue #13 · tc39/proposal-decimal
November 13, 2019 - Java-like: Require that the user provide the precision and/or rounding mode as a parameter when performing division · Ruby-like: Just choose an arbitrary amount of extra decimal places to use for the division result · The implicit third option ...
Published   Nov 13, 2019
Find elsewhere
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.

🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › math › BigDecimal.html
BigDecimal (Java SE 9 & JDK 9 )
If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. ... ArithmeticException - if divisor is zero, roundingMode==RoundingMode.UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly.
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.divide java code examples | Tabnine
/** * 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5 */ public static BigDecimal fen2yuan(BigDecimal num) { return num.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP); }
🌐
RoseIndia
roseindia.net › java › java-bigdecimal › bigDecimal-divide-int.shtml
Java BigDecimal divide examples
Example contains scale feature ... quotient will be specified in the program. Also in the example, bigdecimal class ROUND_FLOOR field is used that defines the Rounding mode to round towards negative infinity....
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. The new divide(BigDecimal, int, RoundingMode) method should be used in preference to this ...
🌐
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. ... The simplest way would be to rearrange the formula. You can change 850 * (a / b) to (850 * a) / b. (Do the multiplication first followed by the division) BigDecimal.divide(BigDecimal.multiply(a,1,RoundingMode.HALF_UP),2,RoundingMode.HALF_UP);
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 17 & JDK 17)
January 20, 2026 - Returns a BigDecimal whose scale ... value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division....
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 23 & JDK 23)
October 17, 2024 - Returns a BigDecimal whose scale ... value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division....
Top answer
1 of 2
6

That's because the division you are computing is made with a scale of 0. Quoting the method divide(divisor, roundingMode) Javadoc:

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

In this case, this.scale() refers to the scale of the numerator, which is 0 because the numerator is BigDecimal.valueOf(n), with n being an integer.

You need to change this division to use divide(divisor, scale, roundingMode) instead and specify the scale you want.

2 of 2
2

From the java doc

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.)

As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3.

If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

To fix, you need to do something like this:

   // numberator / denominator (round down)
        probT = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 10,RoundingMode.DOWN);

where 10 is precision(decimal places precision) and RoundingMode.DOWN is rounding mode

🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 22 & JDK 22)
July 16, 2024 - Returns a BigDecimal whose scale ... value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division....