One important point that is alluded to but not directly addressed is the difference between "precision" and "scale" and how they are used in the two statements. "precision" is the total number of significant digits in a number. "scale" is the number of digits to the right of the decimal point.

The MathContext constructor only accepts precision and RoundingMode as arguments, and therefore scale is never specified in the first statement.

setScale() obviously accepts scale as an argument, as well as RoundingMode, however precision is never specified in the second statement.

If you move the decimal point one place to the right, the difference will become clear:

// 1.
new BigDecimal("35.3456").round(new MathContext(4, RoundingMode.HALF_UP));
//result = 35.35
// 2.
new BigDecimal("35.3456").setScale(4, RoundingMode.HALF_UP);
// result = 35.3456
Answer from dale peters on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-setscale-method-in-java-with-examples
BigDecimal setScale() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Below programs is used to illustrate the setScale() method of BigDecimal. ... // Java program to demonstrate // setScale() method of BigDecimal import java.math.BigDecimal; public class GFG { public static void main(String[] args) { // BigDecimal object to store the result BigDecimal res; // For user input // Use Scanner or BufferedReader // Object of String created // Holds the value String input1 = "31452678569.25"; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); // Scale for BigDecimal int newScale = 4; // Using setScale() method res = a.setScale(newScale); // Display the result in BigDecimal System.out.println(res); } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - The new setScale(int, RoundingMode) method should be used in preference to this legacy method. ... 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 ...
Discussions

BigDecimal scale and rounding confusion
I would expect the first result to be 55.32. Please run BigDecimal bigDecimal = new BigDecimal(55.325); System.out.println(bigDecimal); You will see it prints 55.3250000000000028421709430404007434844970703125 which is due to converting the number to a double and then to a decimal. (see Javadoc for details and for why you probably don't want to use that BigDecimal constructor.) As that's above the half-point it will round up. You'll probably want to use new BigDecimal("55.325") More on reddit.com
🌐 r/javahelp
16
10
July 20, 2022
SOLVED: How to round up a BigDecimal - Products - Jaspersoft Community
Hi,I want a value of 3.665 to be rounded to 3.67.I have a field provided with a BigDecimal. The field is in a crosstab. I've set the field to show 2 decimal places. The problem I am having is that it doesn't seem to round correctly.In the fields textfield I have:$V{gpaAverageMeasure}.setScale(2, ... More on community.jaspersoft.com
🌐 community.jaspersoft.com
July 10, 2015
Simplifying my ugly BigDecimal math

I'm probably not qualified to comment, since I'm new to Java, but why not use the double data type? Here's how I would solve the problem:

+u/CompileBot JAVA

public static double total(int quarters, int dimes, int nickels) {
    double result;     
    double qValue = (0.25)*quarters;
    double dValue = (0.10)*dimes;
    double nValue = (0.05)*nickels;
    result = qValue + dValue + nValue;
    return result;
}
public static void main(String[] args){
    System.out.printf("Total: $%.2f\n",total(4,5,5));
}
More on reddit.com
🌐 r/learnjava
5
1
March 5, 2012
Quickest way to get 2 decimal places from BigDecimal?
Hi, I'm using Java 1.6. For the purposes of writing the "cents" from a BigDecimal in a special way on a JSP page, how do I extract only the two decimal places from a BigDecimal number? For example, if my number is 123.45123 I want to get the number "45" (the two numbers after the decimal... More on thecodingforums.com
🌐 thecodingforums.com
11
July 17, 2010
🌐
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 - Since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.
🌐
Codemia
codemia.io › knowledge-hub › path › bigdecimal_setscale_and_round
BigDecimal setScale and round
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.setscale
BigDecimal.SetScale Method (Java.Math) | Microsoft Learn
[<Android.Runtime.Register("setScale", "(ILjava/math/RoundingMode;)Ljava/math/BigDecimal;", "GetSetScale_ILjava_math_RoundingMode_Handler")>] abstract member SetScale : int * Java.Math.RoundingMode -> Java.Math.BigDecimal override this.SetScale : int * Java.Math.RoundingMode -> Java.Math.BigDecimal
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal setscale method in java
BigDecimal setScale Method in Java
September 1, 2008 - Since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated · Following is the declaration for java.math.BigDecimal.setScale() method.
Find elsewhere
🌐
Andres Jaimes
andres.jaimes.net › 1351 › using-java-bigdecimal
Quick lessons on working with Java’s BigDecimal | Andres Jaimes
May 13, 2018 - 1package test; 2 3import java.math.BigDecimal; 4import java.math.RoundingMode; 5 6public class Test { 7 8 public static void main(String[] args) { 9 BigDecimal a = new BigDecimal("1.204165"); 10 System.out.println("a (from string): " + a); 11 // 1.204165 12 13 a = new BigDecimal(1); 14 System.out.println("a (from int): " + a); 15 // 1 16 17 a = new BigDecimal(1.204165); 18 System.out.println("a (from float): " + a); 19 // 1.2041649999999999298694319804781116545200347900390625 20 21 a = a.setScale(6, RoundingMode.HALF_UP); 22 System.out.println("a (setting scale): " + a); 23 // 1.204165 24 25 S
🌐
Reddit
reddit.com › r/javahelp › bigdecimal scale and rounding confusion
r/javahelp on Reddit: BigDecimal scale and rounding confusion
July 20, 2022 -

BigDecimal(55.325).setScale(2,RoundingMode.HALF_DOWN) //55.33 BigDecimal(55.355).setScale(2,RoundingMode.HALF_DOWN) //55.35

I would expect the first result to be 55.32. As per the second example it is rounded down to 55.35

🌐
Scaler
scaler.com › topics › bigdecimal-java
BigDecimal Java - Scaler Topics
May 3, 2023 - The BigDecimal class in Java consists of a 32-bit integer scale and a random precision integer scale. If the scale is positive or zero, the number of digits to the right of the decimal point is used.
🌐
IncludeHelp
includehelp.com › java › bigdecimal-setscale-method-with-example.aspx
Java BigDecimal setScale() Method with Example
In all the cases, the return type of the method is BigDecimal, it returns the BigDecimal object and its scale value is set by the given parameter and its unscale value is calculated by either divide or multiply this BigDecimal unscaled value by the power of 10.
🌐
JetBrains
jetbrains.com › help › inspectopedia › BigDecimalLegacyMethod.html
'BigDecimal' legacy method called | Inspectopedia Documentation
December 3, 2025 - new BigDecimal("42").setScale(2, RoundingMode.FLOOR); 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. BigDecimalLegacyMethod · 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 | Java language level migration aids | Java 5 ·
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - For example, the BigDecimal 3.14 has an unscaled value of 314 and a scale of 2.
🌐
Jaspersoft Community
community.jaspersoft.com › forum
SOLVED: How to round up a BigDecimal - Products - Jaspersoft Community
July 10, 2015 - Hi,I want a value of 3.665 to be rounded to 3.67.I have a field provided with a BigDecimal. The field is in a crosstab. I've set the field to show 2 decimal places. The problem I am having is that it doesn't seem to round correctly.In the fields textfield I have:$V{gpaAverageMeasure}.setScale(2, ...
🌐
Reddit
reddit.com › r/learnjava › simplifying my ugly bigdecimal math
r/learnjava on Reddit: Simplifying my ugly BigDecimal math
March 5, 2012 -

I'm learning monetary calculations with BigDecimal. My method is simple. Take the number of quarters, dimes and nickels and return the monetary value (ie 4 quarters + 5 dimes + 5 nickels = $1.75). Simple enough. My method works, so no problem there, but when I look at it, the code block looks heavy. I'm sure it can be cleaned up.

public static BigDecimal total (int quarters, int dimes, int nickels) {
	BigDecimal qValue = new BigDecimal ("0.25").setScale(2);
	BigDecimal dValue = new BigDecimal ("0.10").setScale(2);
	BigDecimal nValue = new BigDecimal ("0.05").setScale(2);
	
	BigDecimal qTotal = qValue.multiply(new BigDecimal(quarters)).setScale(2);
	BigDecimal dTotal = dValue.multiply(new BigDecimal(dimes)).setScale(2);
	BigDecimal nTotal = nValue.multiply(new BigDecimal(nickels)).setScale(2);
	
	BigDecimal total = qTotal.add( dTotal.add(nTotal)).setScale(2);
	
	return total;	 
}

So my question is, what would you do to simplify this? Is .setScale necessary in this example? Is it necessary for each bd? Also, I'm not quite sure how to word this, but should I not declare each bd the way I do? Could I sort of nest each bd declaration inside of total?

EDIT: Just went back into Eclipse to play around with the "nesting" idea and it just seems WAY too difficult to read. I didn't get it to work simply because I couldn't follow the logic of how it would be done.

Top answer
1 of 2
1

I'm probably not qualified to comment, since I'm new to Java, but why not use the double data type? Here's how I would solve the problem:

+u/CompileBot JAVA

public static double total(int quarters, int dimes, int nickels) {
    double result;     
    double qValue = (0.25)*quarters;
    double dValue = (0.10)*dimes;
    double nValue = (0.05)*nickels;
    result = qValue + dValue + nValue;
    return result;
}
public static void main(String[] args){
    System.out.printf("Total: $%.2f\n",total(4,5,5));
}
2 of 2
1

You're right about the BigDecimal(double val) constructor not being precise enough. From the API; emphasis, mine:

public BigDecimal(double val)

░░░░Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.

Also, in that Javadoc:

public BigDecimal(int val)

░░░░Translates an int into a BigDecimal. The scale of the BigDecimal is zero.

public BigDecimal multiply(BigDecimal multiplicand)

░░░░Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).


To sum up, this means if you created your 'value' variables like so:

BigDecimal qValue = new BigDecimal ("0.25");

they would automatically have a 'scale' of 2 so you wouldn't need to set it. Likewise, if you kept your assignment of other variables as

BigDecimal qTotal = qValue.multiply(new BigDecimal(quarters));

qTotal would have a scale of 2 because your scales are 2 and 0, respectively;


Also, I know that this wasn't part of your question, but I would suggest changing your qValue, dValue, and nValue variables to constants like

private static final BigDecimal QUARTER_VALUE = new BigDecimal(".25");

Nothing wrong with how it is. It's probably not even better my way, but it's just a preference of mine.

🌐
The Coding Forums
thecodingforums.com › archive › archive › java
Quickest way to get 2 decimal places from BigDecimal? | Java | Coding Forums
July 17, 2010 - Does x.reminder(BigDecimal.ONE).setScale(2) work? Click to expand... No. Exception in thread "main" java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1435) at java.math.BigDecimal.setScale(BigDecimal.java:2381) at java.math.BigDecimal.setScale(BigDecimal.java:2428) at test.BigDecimalTest.main(BigDecimalTest.java:26) Java Result: 1 ·
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › static analysis and remediation › `bigdecimal` rounding constants to `roundingmode` enums
BigDecimal rounding constants to RoundingMode enums | OpenRewrite Docs
import java.math.BigDecimal; class A { void divide() { BigDecimal bd = BigDecimal.valueOf(10); BigDecimal bd2 = BigDecimal.valueOf(2); BigDecimal bd3 = bd.divide(bd2, BigDecimal.ROUND_DOWN); bd.divide(bd2, 1); bd.divide(bd2, 1, BigDecimal.ROUND_CEILING); bd.divide(bd2, 1, 1); bd.setScale(2, 1); } } import java.math.BigDecimal; import java.math.RoundingMode; class A { void divide() { BigDecimal bd = BigDecimal.valueOf(10); BigDecimal bd2 = BigDecimal.valueOf(2); BigDecimal bd3 = bd.divide(bd2, RoundingMode.DOWN); bd.divide(bd2, RoundingMode.DOWN); bd.divide(bd2, 1, RoundingMode.CEILING); bd.div
Published   November 27, 2025