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 OverflowGeeksforGeeks
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, RoundingMode roundingMode) Parameters: This method accepts two parameters: ... roundingMode of type RoundingMode that tells which rounding mode to apply.
Top answer 1 of 2
108
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
2 of 2
-1
import java.math.*;
class Main{
public static void main(String[] args) {
// create 3 BigDecimal objects
BigDecimal bg1, bg2, bg3;
MathContext mc=new MathContext(10,RoundingMode.DOWN);
bg1 = new BigDecimal("2.4",mc);
bg2 = new BigDecimal("32301",mc);
bg3 = bg1.divide(bg2,mc); // divide bg1 with bg2
String str = "Division result is " +bg3;
// print bg3 value
System.out.println( str );
}
}
giving wrong answer
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - In the case of divide, the exact ... 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.
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal divide with rounding modes and scale
BigDecimal Division in Java: Rounding Modes & Scale
September 1, 2008 - The following example shows the ... bg1 = new BigDecimal("16"); bg2 = new BigDecimal("3"); // divide bg1 with bg2 with 3 scale bg3 = bg1.divide(bg2, 3, RoundingMode.CEILING); String str = "Division result is " +bg3; // print ...
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
In the case of divide, the exact ... 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.
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.divide
BigDecimal.Divide Method (Java.Math) | Microsoft Learn
Java documentation for java.math.BigDecimal.divide(java.math.BigDecimal, java.math.RoundingMode). Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). [Android.Runtime.Register("divide", "(Ljava/math/BigDecimal;I)Ljava/math/BigDecimal;", "GetDivide_Ljava_math_BigDecimal_IHandler")] public virtual Java.Math.BigDecimal?
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_divide_roundingmode.htm
Java.math.BigDecimal.divide() Method
The following example shows the usage of math.BigDecimal.divide() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 3 BigDecimal objects BigDecimal bg1, bg2, bg3; bg1 = new BigDecimal("16"); bg2 = new ...
RoseIndia
roseindia.net › java › java-bigdecimal › bigDecimal-divide-int.shtml
Java BigDecimal divide examples
bigdecimal_objectName = bigdecimal_objectDividendName.bigdecimal.divide(bigdecimal_objectDivisorName, int scale, int roundingMode); Java_BigDecimal_divide_int_scale_int_roundingMode.java
Javatpoint
javatpoint.com › java-math-bigdecimal-divide-method
Java Math.BigDecimal.divide() Method with Examples - Javatpoint
Java BigDecimal class · abs() add() divide() doubleValue() equals() floatValue() hashCode() intValue() intValueExact() max() min() movePointLeft() movePointRight() multiply() negate() Big Integer Class ·
Java2Blog
java2blog.com › home › core java › bigdecimal › bigdecimal divide
BigDecimal divide - Java2Blog
January 11, 2021 - In this tutorial we will see about BigDecimal‘s divide method. BigDecimal’s divide method is used to divide one BigDecimal by another. You can specify scale and rounding mode to get the output according to your need.
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 - 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.
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 - 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.
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-divideandremainder-method-in-java-with-examples
BigDecimal divideAndRemainder() Method in Java with Examples - GeeksforGeeks
July 12, 2025 - Program 1: ... // Java program ... BufferedReader // Two objects of String created // Holds the values String input1 = "4568652655"; String input2 = "2562"; // Convert the string input to BigDecimal BigDecimal a = new ...
Oracle
docs.oracle.com › javase › 9 › docs › api › java › math › BigDecimal.html
BigDecimal (Java SE 9 & JDK 9 )
In the case of divide, the exact ... 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.
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 21 & JDK 21)
January 20, 2026 - 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.
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 - 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.
Coderanch
coderanch.com › t › 557228 › java › Divide-BigDecimal-Integer
Divide BigDecimal by Integer (Java in General forum at Coderanch)
Ravi Kanth D wrote:I have a BigDecimal variable that I need to divide by Integer variable, below is my code, please let me know what is wrong. varBigDecimal1.divide(BigDecimal.valueOf(varInteger1.doubleValue())) There's nothing intrinsically wrong with it, but the redundant double conversion will slow things down. varBigDecimal1.divide(BigDecimal.valueOf(varInteger1)) will work just as well.
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › math › class-use › BigDecimal.html
Uses of Class java.math.BigDecimal (Java SE 19 & JDK 19 [build 1])
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.