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 - ... // Java program to demonstrate ... // Holds the values to calculate the difference String input1 = "545456468445645468464645"; String input2 = "425645648446468486486452"; // Convert the string input to BigDecimal BigDecimal ...
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_subtract_mc.htm
Java.math.BigDecimal.subtract() Method
package com.tutorialspoint; import ... 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 ...
🌐
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.
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.subtract java code examples | Tabnine
if (to instanceof BigDecimal) { BigDecimal to1 = (BigDecimal) to; if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i); throw new GroovyRuntimeException("The argument (" + to + ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else if (to instanceof BigInteger) { BigDecimal to1 = new BigDecimal((BigInteger) to); if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i); throw new GroovyRuntimeException("The argument (" + to + ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else { BigDecimal to1 = new BigDecimal(to.toString()); if (self.compareTo(to1) >= 0) { for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) { closure.call(i);
🌐
RoseIndia
roseindia.net › java › java-bigdecimal › subtract-method.shtml
Java bigdecimal subtract method example
This bigdecimal class method subtracts the object value( in which the method is invoked) by the value of object specified. In shortly, method subtracts the specified object value from this.object value. In the example MathContext class mc object defines the rounding behavior of result.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.subtract
BigDecimal.Subtract Method (Java.Math) | Microsoft Learn
Java.Math · Assembly: Mono.Android.dll · Important · Some information relates to prerelease product that may be substantially modified before it’s released. 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. [Android.Runtime.Register("subtract", "(Ljava/math/BigDecimal;Ljava/math/MathContext;)Ljava/math/BigDecimal;", "GetSubtract_Ljava_math_BigDecimal_Ljava_math_MathContext_Handler")] public virtual Java.Math.BigDecimal?
🌐
Simplesolution
simplesolution.dev › java-code-examples › java.math.bigdecimal.subtract
How to Subtract two BigDecimal values in Java
package simplesolution.dev; import ... args) { BigDecimal bigDecimal1 = new BigDecimal("20.02"); BigDecimal bigDecimal2 = new BigDecimal("10.01"); BigDecimal result = bigDecimal1.subtract(bigDecimal2); System.out.printl...
🌐
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).
Find elsewhere
🌐
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).
🌐
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).
🌐
Coderanch
coderanch.com › t › 685804 › java › Big-Decimal-numbers-subtraction-working
Big Decimal numbers subtraction is not working (Java in General forum at Coderanch)
In below code I am just subtracting ...gDecimal.html wrote:subtract(BigDecimal subtrahend, MathContext mc) Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings....
🌐
CodingTechRoom
codingtechroom.com › question › how-to-subtract-bigdecimal-java
How to Perform Subtraction with BigDecimal in Java? - CodingTechRoom
BigDecimal a = new BigDecimal("10.5"); BigDecimal b = new BigDecimal("2.3"); BigDecimal result = a.subtract(b); // result will be 8.2 · In Java, `BigDecimal` is used for high-precision arithmetic without the issues of floating-point representation.
🌐
ConcretePage
concretepage.com › java › java-bigdecimal-tutorial-with-example
BigDecimal in Java
SubtractDemo.java · package ... { 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, ...
🌐
Silicon Cloud
silicloud.com › home › java bigdecimal subtraction tutorial
Java BigDecimal Subtraction Tutorial - Blog - Silicon Cloud
August 2, 2025 - import java.math.BigDecimal; public class Main { public static void main(String[] args) { BigDecimal num1 = new BigDecimal("10.5"); BigDecimal num2 = new BigDecimal("5.2"); BigDecimal result = num1.subtract(num2); System.out.println("Subtraction result: " + result); } } In the example above, ...
🌐
TutorialsPoint
tutorialspoint.com › home › articles on trending technologies › subtract one bigdecimal from another bigdecimal in java
Subtract one BigDecimal from another BigDecimal in Java
July 30, 2019 - Use the subtract method to subtract one BigDecimal to another in Java. TheBigDecimal.subtract(BigDecimal val) returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
🌐
Unicode
unicode-org.github.io › icu-docs › apidoc › dev › icu4j › com › ibm › icu › math › BigDecimal.html
BigDecimal (ICU4J 78)
Returns a plain BigDecimal whose value is this-rhs, using fixed point arithmetic. The same as subtract(BigDecimal, MathContext), where the BigDecimal is rhs, and the context is new MathContext(0, MathContext.PLAIN).
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - Examples: Input : double a=0.03; double b=0.04; double c=b-a; System.out.println(c); Output :0.009999999999999998Input : BigDecimal _a = new BigDecimal("0.03"); BigDecimal _b = new BigDecimal("0.04"); BigDecimal _c = _b.subtract(_a); ...
🌐
ZetCode
zetcode.com › java › bigdecimal
Java BigDecimal - high-precision calculations in Java with BigDecimal
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. ... import java.math.BigDecimal; import java.math.RoundingMode; void main() { var val1 = new BigDecimal("3.44"); var val2 = new BigDecimal("2.74"); BigDecimal res1 = val1.add(val2); System.out.println(res1); BigDecimal res2 = val1.subtract(val2); System.out.println(res2); BigDecimal res3 = val1.multiply(val2); System.out.println(res3); BigDecimal res4 = val1.divide(BigDecimal.TEN, RoundingMode.DOWN); System.out.println(res4); BigDecimal res5 = val1.divide(val2, 15, RoundingMode.HALF_UP); System.out.println(res5); }
🌐
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