Here is a solution I found, it seems to be useful for all kind of Object to primitive types.

public static BigDecimal getBigDecimal( Object value ) {
    BigDecimal ret = null;
    if( value != null ) {
        if( value instanceof BigDecimal ) {
            ret = (BigDecimal) value;
        } else if( value instanceof String ) {
            ret = new BigDecimal( (String) value );
        } else if( value instanceof BigInteger ) {
            ret = new BigDecimal( (BigInteger) value );
        } else if( value instanceof Number ) {
            ret = new BigDecimal( ((Number)value).doubleValue() );
        } else {
            throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
        }
    }
    return ret;
}

here is the link: http://www.java2s.com/Code/Java/Data-Type/ConvertObjecttoBigDecimal.htm

Answer from efirat on Stack Overflow
🌐
Codes heaven.......
pankajchunchun.wordpress.com › 2012 › 10 › 10 › convert-cast-object-into-bigdecimal-or-biginteger
Convert/ Cast Object into BigDecimal or BigInteger. | Codes heaven.......
October 10, 2012 - " + e); } catch (ClassCastException e) { System.out.println("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal. " + e); } catch (Exception e) { System.out.println("Exception caught. " + e); } } return result; } ... public static BigInteger getBigInteger(Object value) { BigInteger result = new BigInteger("0"); if(value != null) { try { if(value instanceof BigInteger) { result = (BigInteger) value; } else if(value instanceof String) { result = new BigInteger((String) value); } else if(value instanceof Number) { result = new BigInteger(String.valueOf(((N
🌐
Baeldung
baeldung.com › home › java › java numbers › convert long to bigdecimal in java
Convert Long to BigDecimal in Java | Baeldung
January 8, 2024 - When we talk about converting Long to BigDecimal, some may think both are in the Number type, so we could probably cast Long to BigDecimal like BigDecimal bd = (BigDecimal) longValue.
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
May 19, 2021 - Using NumberFormat.getCurrencyInstance(Locale.FRANCE) applies these formatting rules automatically, allowing the parser to interpret “348,45 €” correctly and produce the expected BigDecimal value of 348.45. By relying on locale-specific NumberFormat instances, we can accurately handle currency strings from different regions, ensuring reliable numeric conversion in internationalized applications. In this article, we learned that Java provides us with multiple methods to convert String to BigDecimal values.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-valueof-method-in-java
BigDecimal valueOf() Method in Java - GeeksforGeeks
April 14, 2022 - // Java Program to Illustrate valueOf() ... "The Converted BigDecimal value is: " + b); } } Output: The java.math.BigDecimal.valueOf(double val) is an inbuilt method in java that translates a double into a BigDecimal, using the double's ...
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › How-to-convert-object-datatype-to-bigdecimal-double › td-p › 2325891
How to convert object datatype to bigdecimal/doubl... - Qlik Community - 2325891
November 16, 2024 - I Tried to use new Bigdecimal(column name).But Its not working. Thanks in advance. ... Ditto - same here! ... Can you try to set the schema in your MSSQL component to Big Decimal there so that it retrieves it as a big decimal? If not, then you need to understand what the object contains first. If it is a String, then you can cast the object to a String first like:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - Note that two BigDecimal objects that are numerically equal but differ in scale (like 2.0 and 2.00) will generally not have the same hash code. ... Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed. A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to ...
Find elsewhere
🌐
Dummies
dummies.com › article › technology › programming-web-design › java › how-to-create-bigdecimal-objects-in-java-153270
How to Create BigDecimal Objects in Java | dummies
July 2, 2025 - The best way to create a BigDecimal object with an initial decimal value is via a string, like this: ... Here, value has a value of exactly 0.01. If the initial value is an integer, you can safely pass it to the constructor. Remember that integers don’t have the same accuracy problems that doubles and floats do. Also, you can convert a BigDecimal to a double solely for the purpose of using the NumberFormat class to format the result.
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.valueOf java code examples | Tabnine
public static BigDecimal castToDecimal(Object val) { if (val == null) { return null; } if (val instanceof BigDecimal) { return (BigDecimal) val; } if (val instanceof String) { return new BigDecimal((String) val); } if (val instanceof Float) { return new BigDecimal((Float) val); } if (val instanceof Double) { return new BigDecimal((Double) val); } return BigDecimal.valueOf(((Number) val).longValue()); } ... Double toBeTruncated = new Double("3.5789055"); Double truncatedDouble = BigDecimal.valueOf(toBeTruncated) .setScale(3, RoundingMode.HALF_UP) .doubleValue();
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal valueof in java
BigDecimal ValueOf in Java
September 1, 2008 - val − double to convert to a BigDecimal. This method returns a BigDecimal whose value is equal to or approximately equal to the value of val. NumberFormatException − If val is infinite or NaN.
🌐
Daily.dev
app.daily.dev › home › collections › converting integer to bigdecimal in java
Converting Integer to BigDecimal in Java | daily.dev
January 15, 2024 - Explore the techniques for converting an Integer to a BigDecimal in Java, including constructor conversion and static factory conversion. Understand the advantages and disadvantages of each approach and the caching mechanism employed by BigDecimal.
🌐
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 - Two BigDecimal objects that are numerically equal but differ in scale (like 2.0 and 2.00) will generally not have the same hash code. ... Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed. A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used).
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Convert Long to BigDecimal in Java - Java Code Geeks
July 14, 2023 - One straightforward way to convert a long to a BigDecimal is by using the BigDecimal class’s constructor that accepts a long value. This constructor takes the long value as an argument and creates a BigDecimal object with the same numerical value.
🌐
Java2Blog
java2blog.com › home › conversion › java string to bigdecimal
Java convert String to BigDecimal
January 12, 2021 - You can simply use BigDecimal ‘s String based constructor to do it. Above constructor convert String representation of BigDecimal to BigDecimal Let’s see this with the help of example:
Top answer
1 of 4
41

This is fine, remember that using the constructor of BigDecimal to declare a value can be dangerous when it's not of type String. Consider the below...

BigDecimal valDouble = new BigDecimal(0.35);
System.out.println(valDouble);

This will not print 0.35, it will infact be...

0.34999999999999997779553950749686919152736663818359375

I'd say your solution is probably the safest because of that.

2 of 4
23

Can we lose precision with toString() method ?

Kind of ... Both Float.toString() and Double.toString() only output the number of digits after the decimal separator, which is required for the output uniquely to correspond to a float or double value.

To use the 0.35 example in david99world's answer, consider the following code:

BigDecimal bd1 = new BigDecimal(0.35);

Number n = 0.35;
BigDecimal bd2 = new BigDecimal(n.toString());

System.out.println(bd1);
System.out.println(bd2);

An intuitive expectation may be that the two BigDecimal instances are identical, but the output shows that they are not:

0.34999999999999997779553950749686919152736663818359375
0.35

The first line is the exact value of the double, since 0.35 cannot be represented exactly. The second line is 0.35, since no more fractional digits are required to represent the distinct value. E.g. the statement 0.34999999999999997779553950749686919152736663818359375 == 0.35 will evaluate to true.

This is actually not a loss of precision when creating the BigDecimal, the uncertainty is already there in your "source" value. The problem is rather that the discrete values possible using e.g. a float or double value as source not necessarily will be represented by the exact equivalent in the BigDecimal instance.

🌐
Programmersought
programmersought.com › article › 6671214837
Object to BigDecimal - Programmer Sought
Adding, subtracting, multiplying, and dividing BigDecimal in Java and converting Object and string to BigDecimal