🌐
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 ...
🌐
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);
🌐
Medium
medium.com › @AlexanderObregon › javas-bigdecimal-divide-method-explained-204688fe717a
Java’s BigDecimal.divide() Method Explained | Medium
January 13, 2025 - The BigDecimal.divide() method in Java is designed to handle division operations with precision, particularly useful for applications requiring exact decimal calculations. It belongs to the BigDecimal class in the java.math package and is commonly used in scenarios where floating-point arithmetic is inadequate due to potential rounding issues.
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal divide with rounding modes and scale
BigDecimal Division in Java: Rounding Modes & Scale
September 1, 2008 - ArithmeticException − If divisor is zero, roundingMode == RoundingMode.UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly · The following example shows the usage of math.BigDecimal.divide() method. package com.tutorialspoint; import java.math.*; import java.lang.*; public class BigDecimalDemo { public static void main(String[] args) { // create 3 BigDecimal objects BigDecimal bg1, bg2, bg3; 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 bg3 value System.out.println( str ); } }
🌐
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 ... · The implicit third option is to avoid division, and instead have a div/mod operation, or a "partition" operation (to split a BigDecimal into an integer number of buckets as equally as ...
Published   Nov 13, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-divide-method-in-java-with-examples
BigDecimal divide() Method in Java with Examples - GeeksforGeeks
July 12, 2025 - Exception: The method throws Arithmetic Exception if the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion. Below programs is used to illustrate the divide() method of BigDecimal. ... // Java program to demonstrate // divide() method of BigDecimal import java.math.*; public class GFG { public static void main(String[] args) { // BigDecimal object to store the result BigDecimal res; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values String input1 = "24536482";
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.divide java code examples | Tabnine
@Override public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions, TypeInfo typeInfo) { List<MutablePair<String, String>> intervals = new ArrayList<>(); DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo)typeInfo; int scale = decimalTypeInfo.getScale(); BigDecimal decimalLower = new BigDecimal(lowerBound); BigDecimal decimalUpper = new BigDecimal(upperBound); BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions), MathContext.DECIMAL64); BigDecimal splitDecimalLower, splitDecimalUpper; for (i
🌐
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 this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied. The new divide(BigDecimal, RoundingMode) method should be used in preference to this legacy method.
🌐
JetBrains
jetbrains.com › help › inspectopedia › BigDecimalMethodWithoutRoundingCalled.html
Call to 'BigDecimal' method without a rounding mode argument | Inspectopedia Documentation
BigDecimal.valueOf(1).divide(BigDecimal.valueOf(3)); By ID · Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings. BigDecimalMethodWithoutRoundingCalled · Via Settings dialog · Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE. Settings or Preferences | Editor | Inspections | Java | Numeric issues ·
🌐
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). 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), with rounding according to the context settings.
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.

🌐
Medium
medium.com › @skywalkerhunter › fix-divide-java-math-bigdecimal-int-in-java-math-bigdecimal-has-been-deprecated-and-why-22fe83b07e9
Fix — divide(java.math.BigDecimal,int) in java.math.BigDecimal has been deprecated — And WHY | by Oli H. | Medium
July 18, 2020 - divide(total, BigDecimal.ROUND_HALF_UP) Press enter or click to view image in full size · A real example. Step #4 — How about the setScale? Warning:(47, 146) java: setScale(int,int) in java.math.BigDecimal has been deprecated · Same trick, search for: BigDecimal.ROUND_HALF_UP ·
🌐
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 › javase › 9 › docs › api › java › math › BigDecimal.html
BigDecimal (Java SE 9 & JDK 9 )
Returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings. ... ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY. ... Deprecated. The method divide(BigDecimal, int, RoundingMode) should be used in preference ...
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_divide_introundingmode_scale.htm
Java.math.BigDecimal.divide() Method
IllegalArgumentException − If roundingMode does not represent a valid rounding mode. 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 BigDecimal("3"); // divide bg1 with bg2 with 4 scale // 1 specifies BigDecimal.ROUND_DOWN bg3 = bg1.divide(bg2, 4, 1); String str = "Division result is " +bg3; // print bg3 value System.out.println( str ); } }
🌐
Mpg
resources.mpi-inf.mpg.de › d5 › teaching › ss05 › is05 › javadoc › java › math › BigDecimal.html
BigDecimal
The call can also be used to reduce the scale if the caller knows that the BigDecimal has sufficiently many zeros at the end of its fractional part (i.e., factors of ten in its integer value) to allow for the rescaling without loss of precision. This method returns the same result as the two argument version of setScale, but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant.
🌐
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 this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied. The new divide(BigDecimal, RoundingMode) method should be used in preference to this legacy method.
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - Just like the other Number classes (Integer, Long, Double, etc.), BigDecimal provides operations for arithmetic and comparison operations. It also provides operations for scale manipulation, rounding, and format conversion. It doesn’t overload the the arithmetic (+, -, /, *) or logical (>. < etc) operators. Instead, we use the corresponding methods – add, subtract, multiply, divide, and compareTo.