Those are two separate questions: "What should I use for BigDecimal?" and "What do I do in general?"

For BigDecimal: this is a bit tricky, because they don't do the same thing. BigDecimal.valueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.println(d).

If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible. This will usually result in a lot more digits being stored than you want. Strictly speaking, it's more correct than valueOf(), but it's a lot less intuitive.

There's a nice explanation of this in the JavaDoc:

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.

In general, if the result is the same (i.e. not in the case of BigDecimal, but in most other cases), then valueOf() should be preferred: it can do caching of common values (as seen on Integer.valueOf()) and it can even change the caching behaviour without the caller having to be changed. new will always instantiate a new value, even if not necessary (best example: new Boolean(true) vs. Boolean.valueOf(true)).

Answer from Joachim Sauer on Stack Overflow
🌐
CodeGym
codegym.cc › java blog › java numbers › bigdecimal in java
BigDecimal in Java
January 15, 2025 - However, we can't achieve what we want by passing the number 3 as an argument to the setScale() method. As mentioned above, BigDecimal is for representing numbers with strict requirements on computational precision.
🌐
IONOS
ionos.com › digital guide › websites › web development › java class: bigdecimal
How to use BigDecimal in Java - IONOS
July 26, 2024 - Next, create the desired value as an argument and pass this to the ap­pro­pri­ate Java con­struc­tor. Once you’ve completed this process, you can use BigDec­i­mals in Java. Within the class, you’ll find various methods, which we’ll explain in more detail in the following section. First, we’re going to import the class and declare two BigDec­i­mal objects: / / Your Java program for the BigDecimal class import java.math.BigDecimal; public class BigDecimalExample { public static void main(String[] args) { / / Create two new BigDecimals BigDecimal ExampleOne = new BigDecimal ("1275936001.744297361"); BigDecimal ExampleTwo = new BigDecimal ("4746691047.132719503"); } }java
🌐
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
March 26, 2016 - To create a BigDecimal object in Java, you call one of the constructors you see below. Each of these constructors takes a value and converts it to a BigDecimal object.
Author:
Top answer
1 of 5
214

Those are two separate questions: "What should I use for BigDecimal?" and "What do I do in general?"

For BigDecimal: this is a bit tricky, because they don't do the same thing. BigDecimal.valueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.println(d).

If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible. This will usually result in a lot more digits being stored than you want. Strictly speaking, it's more correct than valueOf(), but it's a lot less intuitive.

There's a nice explanation of this in the JavaDoc:

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.

In general, if the result is the same (i.e. not in the case of BigDecimal, but in most other cases), then valueOf() should be preferred: it can do caching of common values (as seen on Integer.valueOf()) and it can even change the caching behaviour without the caller having to be changed. new will always instantiate a new value, even if not necessary (best example: new Boolean(true) vs. Boolean.valueOf(true)).

2 of 5
64

If you are using your BigDecimal objects to store currency values, then I strongly recommend that you do NOT involve any double values anywhere in their calculations.

As stated in another answer, there are known accuracy issues with double values and these will come back to haunt you big time.

Once you get past that, the answer to your question is simple. Always use the constructor method with the String value as the argument to the constructor, as there is no valueOf method for String.

If you want proof, try the following:

Use with AIBigDecimal bd1 = new BigDecimal(0.01);
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("bd1 = " + bd1);
System.out.println("bd2 = " + bd2);

You'll get the following output:

Use with AIbd1 = 0.01000000000000000020816681711721685132943093776702880859375
bd2 = 0.01

See also this related question

🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - BigDecimal setScale​(int newScale, RoundingMode roundingMode): This method returns 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 ...
🌐
Piotr Horzycki
peterdev.pl › all-you-need-to-know-about-javas-bigdecimal
All you need to know about Java’s BigDecimal | Piotr Horzycki - Java and PHP developer’s blog
February 11, 2021 - final BigDecimal net = new BigDecimal("123.45"); final BigDecimal taxRate = new BigDecimal("0.23"); final BigDecimal tax = net.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); final BigDecimal gross = net.add(tax); final NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.forLanguageTag("en_US")); numberFormat.setCurrency(Currency.getInstance("USD")); final NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.forLanguageTag("en_US")); System.out.println("Net value: " + numberFormat.format(net)); System.out.println("Tax value: " + numberFormat.format(tax)); Sy
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - To convert a double or long to BigDecimal, we can use the valueOf() static method.
🌐
Java67
java67.com › 2020 › 03 › simple-example-of-bigdecimal-in-java.html
How to use BigDecimal in Java? Example | Java67
You may assume that creating 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.
🌐
Codementor
codementor.io › community › bigdecimal class in java
BigDecimal Class in Java | Codementor
June 23, 2024 - There are two main ways of creating a BigDecimal: using constructors and using static methods BigDecimal#valueOf(). BigDecimal offers multiple constructors with different parameter types: BigInteger, long, double, char, String, etc.
Find elsewhere
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Java BigDecimal Example - Java Code Geeks
May 7, 2021 - If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The different constructors available for BigDecimal class are shown below · This accepts the BigInteger value and converts it into a BigDecimal value
🌐
Kevin's Guides
kevinsguides.com › guides › code › java › javaintro › c5-currency-bigdecimals
Chapter 5: BigDecimals, Mutability & Basic Memory Concepts - Kevin’s Guides
December 6, 2025 - Although you can create BigDecimals with double values like this: new BigDecimal(3.14), since doubles are not 100% precise, it’s better to use a String like ("3.14"). This ensures the value will not be distorted like the float in the first example. We can print out the values of BigDecimals the same way we printed out other types of variables. ... import java.math.BigDecimal; public class Main { public static void main(String[] args) { BigDecimal myBigDecimal = new BigDecimal("3.14"); System.out.println("The value of myBigDecimal is: "+myBigDecimal); } }Code language: JavaScript (javascript)
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_valueof_long.htm
java.math.BigDecimal.valueOf(long val) Method
package com.tutorialspoint; import ... Object Long l = new Long("12345678"); // assign the bigdecimal value of l to bg // static method is called using class name bg = BigDecimal.valueOf(l); String str = "The Long After Conversion ...
🌐
Rip Tutorial
riptutorial.com › initialization of bigdecimals with value zero, one or ten
Java Language Tutorial => Initialization of BigDecimals with value...
By using the static properties, you avoid an unnecessary instantiation, also you've got a literal in your code instead of a 'magic number'. //Bad example: BigDecimal bad0 = new BigDecimal(0); BigDecimal bad1 = new BigDecimal(1); BigDecimal bad10 = new BigDecimal(10); //Good Example: BigDecimal good0 = BigDecimal.ZERO; BigDecimal good1 = BigDecimal.ONE; BigDecimal good10 = BigDecimal.TEN; ... Get monthly updates about new articles, cheatsheets, and tricks. ... This website stores cookies on your computer. We use cookies to enhance your experience on our website and deliver personalized content.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-valueof-method-in-java
BigDecimal valueOf() Method in Java - GeeksforGeeks
April 14, 2022 - ... // Java Program to Illustrate ... Long ln = new Long("745812345678"); // Assigning above long value to BigDecimal BigDecimal b = BigDecimal.valueOf(ln); // Printing the BigDecimal value on console System.out.println( "The Converted ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
🌐
TutorialsPoint
tutorialspoint.com › set-decimal-place-of-a-big-decimal-value-in-java
Set Decimal Place of a Big Decimal Value in Java
July 30, 2019 - import java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { int decPlaces1 = 3; int decPlaces2 = 5; BigDecimal val1 = new BigDecimal("37578975587.876876989"); BigDecimal val2 = new BigDecimal("62567875598.976876569"); System.out.println("Value 1 : "+val1); System.out.println("Value 2 : "+val2); // ROUND_DOWN val1 = val1.setScale(decPlaces1, BigDecimal.ROUND_DOWN); String str1 = val1.toString(); System.out.println("Result = "+str1); // ROUND_UP val2 = val2.setScale(decPlaces2, BigDecimal.ROUND_UP); String str2 = val2.toString(); System.out.println("Result = "+str2); } }