BigDecimal.valueOf(double d) uses canonical String representation of double value, internally Double.toString(double) is used, that's why you are getting 0.9 in second case.

Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

While with new BigDecimal(0.9) it converts value to exact floating point representation of double value without using String representation,

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.

...

NOTES :

  1. The results of this constructor can be somewhat unpredictable.

...

FOR EXAMPLE :

BigDecimal bd1 = new BigDecimal(Double.toString(0.9));
BigDecimal bd2 = new BigDecimal(0.9);
System.out.println(bd1);
System.out.println(bd2);

OUTPUT :

0.9
0.90000000000000002220446049250313080847263336181640625
Answer from Akash Thakare on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-subtract-method-in-java-with-examples
BigDecimal subtract() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - Below programs is used to illustrate the subtract() method of BigDecimal. ... // Java program to demonstrate // subtract() method of BigDecimal import java.math.BigDecimal; public class GFG { public static void main(String[] args) { // BigDecimal object to store result BigDecimal diff; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values to calculate the difference String input1 = "545456468445645468464645"; String input2 = "425645648446468486486452"; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); BigDecimal b = new BigDecimal(input2); // Using subtract() method diff = a.subtract(b); // Display the result in BigDecimal System.out.println("The difference of\n" + a + " \nand\n" + b + " " + "\nis\n" + diff + "\n"); } } Output:
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_subtract_mc.htm
Java.math.BigDecimal.subtract() Method
The following example shows the usage of math.BigDecimal.subtract() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 3 BigDecimal objects BigDecimal bg1, bg2, bg3; MathContext mc = new MathContext(2); // 2 precision bg1 = new BigDecimal("100.123"); bg2 = new BigDecimal("50.56"); // subtract bg1 with bg2 using mc and assign result to bg3 bg3 = bg1.subtract(bg2, mc); String str = "The Result of Subtraction is " + bg3; // print bg3 value System.out.println( str ); } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale. If n is negative, the call is equivalent to movePointLeft(-n).
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.subtract
BigDecimal.Subtract Method (Java.Math) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.
🌐
Simplesolution
simplesolution.dev › java-code-examples › java.math.bigdecimal.subtract
How to Subtract two BigDecimal values in Java
package simplesolution.dev; import java.math.BigDecimal; public class BigDecimalSubtractExamples { public static void main(String... args) { BigDecimal bigDecimal1 = new BigDecimal("20.02"); BigDecimal bigDecimal2 = new BigDecimal("10.01"); BigDecimal result = bigDecimal1.subtract(bigDecimal2); System.out.println(bigDecimal1); System.out.println(bigDecimal2); System.out.println(result); } }
🌐
IncludeHelp
includehelp.com › java › bigdecimal-subtract-method-with-example.aspx
Java BigDecimal subtract() Method with Example
In the first case, subtract(BigDecimal val, MathContext ma_co), BigDecimal val – Similar as defined in the first case. MathContext ma_co – represents the context setting to use in rounding. ... In the first case, it returns the value substracted the given parameter from this object. In the second case, it returns the value substracted the given parameter from this object with rounding "NECESSARY". ... // Java program to demonstrate the example // of subtract() method of BigDecimal import java.math.*; public class SubstractOfBD { public static void main(String args[]) { // Initialize two va
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 6)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale. If n is negative, the call is equivalent to movePointLeft(-n).
Find elsewhere
🌐
Jaspersoft Community
community.jaspersoft.com › forum
How to add/subtract multiple big decimals - Products - Jaspersoft Community
March 20, 2020 - Hello,I am new to Jaspersoft Studio. I have a variable that is big decimal called $V{cs_bal}. I need to perform several calculations to get the result. All variables and column amounts are big decimal. In common terms:if column 8 > 0then var(amt_due) + var(pdl_amt) - column 8else var(amt_due) ...
🌐
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 which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale. If n is negative, the call is equivalent to movePointLeft(-n).
🌐
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.
🌐
Codota
codota.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.subtract java code examples | Codota
private BigDecimal calcTotalAmount(List<CartPromotionItem> cartItemList) { BigDecimal total = new BigDecimal("0"); for (CartPromotionItem item : cartItemList) { BigDecimal realPrice = item.getPrice().subtract(item.getReduceAmount()); total=total.add(realPrice.multiply(new BigDecimal(item.getQuantity()))); } return total; }
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale. If n is negative, the call is equivalent to movePointLeft(-n).
🌐
ZetCode
zetcode.com › java › bigdecimal
Java BigDecimal - high-precision calculations in Java with BigDecimal
July 4, 2024 - BigDecimal is used in areas where high-precision is necessary; for instance, financial or monetary calculations. The arithmetic operations of BigDecimal values are done with methods such as add or subtract; the +, -, *, \ operators are not overloaded. The following example shows basic arithmetic operations with BigDecimal. Main.java ·
🌐
Program Creek
programcreek.com › java-api-examples
java.math.BigDecimal#subtract
@Test public void testTransfer() throws Exception { BigDecimal acct1start = service.getBalance(1L); BigDecimal acct2start = service.getBalance(2L); BigDecimal amount = new BigDecimal("50.0"); service.transfer(1L, 2L, amount); BigDecimal acct1finish = acct1start.subtract(amount); BigDecimal acct2finish = acct2start.add(amount); assertThat(acct1finish, is(closeTo(service.getBalance(1L), new BigDecimal("0.01")))); assertThat(acct2finish, is(closeTo(service.getBalance(2L), new BigDecimal("0.01")))); }
🌐
RoseIndia
roseindia.net › java › java-bigdecimal › subtract-method.shtml
Java bigdecimal subtract method example
Syntax for using the method: public BigDecimal subtract(BigDecimal subtrahend, mc) Suppose three bigdecimal objects x, y & z then z = x.subtract(y, mc); System.out.println(z);
🌐
Mkyong
mkyong.com › home › java › how to calculate monetary values in java
How to calculate monetary values in Java - Mkyong.com
February 24, 2020 - package com.mkyong; import java.math.BigDecimal; import java.math.RoundingMode; public class JavaMoney2 { public static void main(String[] args) { System.out.println("--- BigDecimal-----"); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.1))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.2))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.3))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.4))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.5))); System.out
🌐
ConcretePage
concretepage.com › java › java-bigdecimal-tutorial-with-example
BigDecimal in Java
SubtractDemo.java · package com.concretepage; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class SubtractDemo { public static void main(String[] args) { BigDecimal b = new BigDecimal("50.345"); BigDecimal bdec = b.subtract(new BigDecimal("34.765")); System.out.println("Subtract: " + bdec); //Using MathContext MathContext mc = new MathContext(2, RoundingMode.FLOOR); BigDecimal bdecMath = b.subtract(new BigDecimal("34.765"), mc); System.out.println("Subtract using MathContext: " + bdecMath); } } Output
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Java BigDecimal Example - Java Code Geeks
May 7, 2021 - This method is used to find out the minimum of two BigDecimal number and returns the minimum of the two BigDecimal number ... This method is used to subtract two BigDecimal number and returns the subtraction result as a BigDecimal number
🌐
Oracle
docs.oracle.com › en › java › javase › 18 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 18 & JDK 18)
August 18, 2022 - Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale. If n is negative, the call is equivalent to movePointLeft(-n).