The reason of such behaviour is that the string that is printed is the exact value - probably not what you expected, but that's the real value stored in memory - it's just a limitation of floating point representation.

According to javadoc, BigDecimal(double val) constructor behaviour can be unexpected if you don't take into consideration this limitation:

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.

So in your case, instead of using

double val = 77.48;
new BigDecimal(val);

use

BigDecimal.valueOf(val);

Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of Double.toString(double).

Answer from omnomnom on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › java double vs. bigdecimal
Java Double vs. BigDecimal | Baeldung
January 8, 2024 - Imagine having a powerful magnifying glass that can zoom in on any part of the number line, allowing us to work with large or incredibly tiny numbers. It consists of two parts: an unscaled value (an integer with arbitrary precision), and the scale (which indicates the number of digits after the decimal point). For example, the BigDecimal 3.14 has an unscaled value of 314 and a scale of 2. The BigDecimal class offers better precision than Double, as it can perform calculations with arbitrary-precision decimals, avoiding the rounding errors arising from Double’s binary representation.
Discussions

Double vs BigDecimal in financial programming

Use long or big decimal. Performance problems will most probably arise elsewhere before you factor a big decimal as the culprit

More on reddit.com
🌐 r/java
105
44
May 22, 2018
Stack overflow error after changing from double to BigDecimal in Java/OOP & Polymorphism
Secondly, my getEmployeeGrossSalary in the superclass RegularEmployees is highlighted and it says that the method recurses infinitely You call the same method again in this method BigDecimal(String.valueOf(this.getEmployeeGrossSalary())) this causes the recursion problem. You need to access the field (employeeGrossSalary instead I suspect) in the class and not the method. Regarding the BigDecimal and + you can't use the operator directly as BigInt is not a primitive data type (like int or double). But the BigDecimal class should have appropriate method to do math with it. I assume its called something like add or subtract. Another thing I wonder is why you save the date in the class as double to then convert it? Instead of just keeping it as BigDecimal always. More on reddit.com
🌐 r/learnprogramming
6
1
January 5, 2022
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_valueof_double.htm
java.math.BigDecimal.valueOf(double val) Method
The java.math.BigDecimal.valueOf(double val) translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.
🌐
javathinking
javathinking.com › blog › convert-double-to-bigdecimal-and-set-bigdecimal-precision
Java: How to Convert double to BigDecimal with Precision and Avoid Scientific Notation — javathinking.com
Naively converting double to BigDecimal (e.g., using new BigDecimal(double)) captures the exact binary representation of the double, which often includes more decimal digits than intended, leading to precision bloat.
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
June 17, 2026 - For example, we can convert a decimal-based long value without removing non-numeric symbols: BigDecimal bigDecimal = new BigDecimal(10692467440017.111).setScale(3, BigDecimal.ROUND_HALF_UP); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); ...
🌐
InfoWorld
infoworld.com › home › software development
Caution: Double to BigDecimal in Java | InfoWorld
January 14, 2010 - At first sight, it might appear that the BigDecimal constructor that accepts a Java double would hold it with its originally specified precision in all cases. However, the Javadoc message for this constructor explicitly warns, “The results of this constructor can be somewhat unpredictable.” It goes on to explain why (the double cannot hold the exact precision and this is made evident when passed to the BigDecimal constructor) and to suggest that the alternative constructor accepting a String as a parameter be used instead.
🌐
Codementor
codementor.io › community › bigdecimal class in java
BigDecimal Class in Java | Codementor
June 23, 2024 - Java also provides static factory methods to create a BigDecimal from a BigInteger, a double, or a long. You should prefer these methods when converting primitive types (long, double) or BigIntegers to BigDecimals.
Find elsewhere
🌐
Reddit
reddit.com › r/java › double vs bigdecimal in financial programming
r/java on Reddit: Double vs BigDecimal in financial programming
May 22, 2018 -

Everyone says that BigDecimal should be used when dealing with money but it’s much slower and takes more memory than double. I would think this would be especially important in high frequency low-latency applications like trading. Do people actually use BigDecimal in such systems or do they use doubles with some kind of workaround to handle the precision issue?

Edit: I do have experience working on trading and risk systems and I see doubles used much more often than BigDecimal so I was curious to see if this is more common in actual practice. Most of the systems I worked on only need precision to the penny so I wonder if that’s the reason?

Also, BigDecimal is a pain to use and code written with it look much uglier than plain doubles.

🌐
Medium
serdaralkancode.medium.com › why-use-bigdecimal-instead-of-double-java-179e51cd1cbc
Why use BigDecimal instead of Double? (Java) | by Serdar ALKAN | Medium
October 1, 2024 - Double r1 : 0.020000000000000004 BigDecimal b3 : 0.02 Double r2 : 0.009999999999999998 BigDecimal b6 : 0.01 Double r3 : 0.09999999999999998 BigDecimal b9 : 0.1
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin › to-big-decimal.html
toBigDecimal | Core API – Kotlin Programming Language
The number is converted to a string and then the string is converted to a BigDecimal. ... Returns the value of this Double number as a BigDecimal.
🌐
Coderanch
coderanch.com › t › 408226 › java › Double-BigDecimal-Conversion-problems
Double to BigDecimal Conversion problems (Beginning Java forum at Coderanch)
If you check the API documentation for BigDecimal's constructors, you will see the difference between creating an instance using a double (whose precision has already been compromised by forcing it into IEEE 754 standards) and creating an instance using a String (which can be translated accurately). This is illustrated with the code below. BigDecimal instances are objects -- not simple values. So you need to perform calculations using method calls -- not simple operators.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - To get that result, use the static valueOf(double) method. ... NumberFormatException - if val is infinite or NaN. ... Translates a double into a BigDecimal, with rounding according to the context settings.
🌐
Reddit
reddit.com › r/learnprogramming › stack overflow error after changing from double to bigdecimal in java/oop & polymorphism
r/learnprogramming on Reddit: Stack overflow error after changing from double to BigDecimal in Java/OOP & Polymorphism
January 5, 2022 -

Hi, So after changing in program the Gross and netSalary to BigDecimal, I got lots of errors and it is not clear how I can fix them. So first of all in the main, I want to print the sum of all netSalaries paid, however when I changed it to BigDecimal it is saying that I cannot apply '+' to java.math.BigDecimal and I really can't find a solution for it. Secondly, my getEmployeeGrossSalary in the superclass RegularEmployees is highlighted and it says that the method recurses infinitely and can only end by throwing an exception and when I run the program I am getting a stackOverflow error because I am calling it in the main to print all salaries from highest to lowest, so why was everything fine when the type was double and how can I fix these errors? Thanks in advance!

import java.math.BigDecimal;
import java.math.RoundingMode;

public class RegularEmployees { //superclass
    protected double employeeGrossSalary;

    RegularEmployees(double employeeGrossSalary) {
        this.employeeGrossSalary = employeeGrossSalary;
    }
 public BigDecimal employeeNetSalary() {
        return new BigDecimal(this.employeeGrossSalary - (this.employeeGrossSalary * 0.1)).setScale(2,RoundingMode.DOWN);
    }
public BigDecimal getEmployeeGrossSalary() { //the method here is highlighted and it says that getEmployeeGrossSalary recurses infinitely and can only end by throwing an exception.
        return new
BigDecimal(String.valueOf(this.getEmployeeGrossSalary())).setScale(2,RoundingMode.DOWN);
    }
}

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ManagerEmployees extends RegularEmployees { //Subclass
    private String managerDegree;

    ManagerEmployees(double employeeGrossSalary, String managerDegree) {
        super(employeeGrossSalary);
this.managerDegree = managerDegree;

public BigDecimal getEmployeeGrossSalary() {
        return new
BigDecimal(String.valueOf(this.getEmployeeGrossSalary())).setScale(2,RoundingMode.DOWN);
    } 
@Override
    public BigDecimal employeeNetSalary(){
        return new BigDecimal(super.employeeGrossSalary - (super.employeeGrossSalary * 0.1)).setScale(2, RoundingMode.DOWN);
    }
@Override
    public BigDecimal getEmployeeGrossSalary() {

        BigDecimal grossSalaryWithBonus = BigDecimal.valueOf(0.00);

        if (this.managerDegree.equals("BSc")) {
            grossSalaryWithBonus = new BigDecimal(super.employeeGrossSalary - (super.employeeGrossSalary * 0.1)).setScale(2, RoundingMode.DOWN);

        } else {

            if (this.managerDegree.equals("MSc")) {
                 grossSalaryWithBonus = new BigDecimal(super.employeeGrossSalary -(super.employeeGrossSalary * 0.2)).setScale(2, RoundingMode.DOWN);

            } else {

                if (this.managerDegree.equals("PhD")) {
                    grossSalaryWithBonus = new BigDecimal(super.employeeGrossSalary + (super.employeeGrossSalary * 0.35)).setScale(2, RoundingMode.DOWN);
                }
            }
        }
        return grossSalaryWithBonus;
    }
}

import java.util.Comparator;
import java.util.ArrayList;
import java.math.BigDecimal;

public class Main { //main class
public static void main(String[] args){

ArrayList<RegularEmployees> employeesList = new ArrayList<>();

BigDecimal totalNetSalaries = BigDecimal.valueOf(0.00);

                    for (RegularEmployees currentEmployee : employeesList) {
                    totalNetSalaries = BigDecimal.valueOf(totalNetSalaries + currentEmployee.employeeNetSalary()); //Bug here ; Operator "+" cannot be applied to  'java.math.BigDecimal','java.math.BigDecimal' how can I fix it?
                  }
                   System.out.println(totalNetSalaries);

employeesList.sort(Comparator.comparing(RegularEmployees::getEmployeeGrossSalary).reversed());
                    for(RegularEmployees current : employeesList){
                    System.out.println(current.getEmployeeGrossSalary());
                    }
🌐
LinkedIn
linkedin.com › pulse › why-we-should-use-bigdecimal-instead-double-java-financial-ismail
Why we should use BigDecimal instead double in java for financial systems
May 17, 2022 - But in financial or any other systems where each digit of decimal part are important, BigDecimal should be chosen over double/float. ... Don’t have the app? Get it in the Microsoft Store. Open the app ... By clicking Continue, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.
🌐
Blogger
javahungry.blogspot.com › 2020 › 01 › string-to-bigdecimal.html
2 ways to Convert String to BigDecimal in Java with Examples | Java Hungry
import java.math.*; public class JavaHungry { public static void main(String args[]) { String str = "123.45"; // Converting String to Double Double obj = new Double(str); // Converting Double to BigDecimal BigDecimal num = BigDecimal.valueOf(obj); // Printing BigDecimal value System.out.println("Converted String to BigDecimal: " + num); } } Output: Converted String to BigDecimal: 123.45
🌐
Medium
medium.com › @reachansari › use-of-bigdecimal-instead-double-in-java-for-financial-systems-591440bdd5f4
Use of BigDecimal instead of double in Java for Financial systems | by Thameem Ansari | Medium
March 22, 2022 - So if we have a system where low latency is crucial than the decimal part of a number, we should go for double. But in financial or any other systems where each digit of decimal part are important, BigDecimal should be chosen over double/float.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-doublevalue-method-in-java
BigDecimal doubleValue() Method in Java - GeeksforGeeks
December 4, 2018 - Examples: Input : 11234 Output : 11234.0 Input : 2679.30000 Output : 2679.3 Below programs illustrates the use of byteValueExact() Function: Program 1: ... // Java program to demonstrate doubleValue() method import java.io.*; import java.math.*; ...
🌐
DEV Community
dev.to › luke_tong_d4f228249f32d86 › beyond-double-essential-bigdecimal-practices-for-accurate-financial-calculations-52m1
Beyond double: Essential BigDecimal Practices for Accurate Financial Calculations - DEV Community
May 28, 2025 - BigDecimal uses concepts of precision (the total number of digits) and scale (the number of digits to the right of the decimal point). The string constructor allows BigDecimal to parse this representation accurately, preserving the intended precision and scale. Sometimes, you might receive a double value from a library or an external system, and you need to convert it to BigDecimal.