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.

Answer from David on Stack Overflow
Top answer
1 of 4
42

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.

🌐
Baeldung
baeldung.com › home › java › java numbers › converting integer to bigdecimal in java
Converting Integer to BigDecimal in Java | Baeldung
June 20, 2024 - BigDecimal is designed to work with large floating point numbers. It solves the problem with floating point arithmetic and provides a way to control the precision.
🌐
CodeGym
codegym.cc › java blog › java numbers › bigdecimal in java
BigDecimal in Java
January 15, 2025 - You can use the setScale() method to set the number of decimal places for a BigDecimal. For example, suppose we want the number 111.5555555555 to have three digits after the decimal point.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - Unlike fixed point numbers, floating point numbers will most of the times return an answer with a small error (around 10^-19) This is the reason why we end up with 0.009999999999999998 as the result of 0.04-0.03 in the above example. But BigDecimal provides us with the exact answer. ... // Java Program to illustrate BigDecimal Class import java.math.BigDecimal; public class BigDecimalExample { public static void main(String[] args) { // Create two new BigDecimals BigDecimal bd1 = new BigDecimal("124567890.0987654321"); BigDecimal bd2 = new BigDecimal("987654321.123456789"); // Addition of two
Top answer
1 of 4
32

To multiply an integer (or byte/short/float/double) with a BigInteger (or BigDecimal), you must convert the native number to BigInteger/BigDecimal first.

// int parameter can be int or Integer
public static BigInteger multiply ( int a, BigInteger b ) {
   return BigInteger.valueOf( a ).multiply( b );
}

// BigInteger <> BigDecimal
public static BigDecimal multiply ( int a, BigDecimal b ) {
   return BigDecimal.valueOf( a ).multiply( b );
}

// same for add, subtract, divide, mod etc.

Note: valueOf is not the same as new, and for different reasons on BigDecimal and BigInteger. In both cases, I recommend valueOf over new.


I see that you added your code, nice. It doesn't work because Integer is mixed with BigDecimal, and also * does not work with BigDecimal. If you compare it with my code, the fix should be obvious:

public BigDecimal methCal ( int quantite, BigDecimal prixUnit ) {
    return BigDecimal.valueOf( quantite ).multiply( prixUnit );
}
2 of 4
8

Google definitely could have helped you, if you know what to look for:

https://docs.oracle.com/javase/9/docs/api/java/math/BigDecimal.html#BigDecimal-int-

This is one of the constructors for BigDecimal, which allows you to do the following:

BigDecimal five = BigDecimal.valueOf(5);
BigDecimal seven = BigDecimal.valueOf(2).add(five);

Seeing as you stated you wanted to multiply an int and a BigDecimal, this would be achieved as follows:

BigDecimal result = yourBigDecimal.multiply(BigDecimal.valueOf(yourInt));

And, supposing you want this result as an int:

int intResult = result.intValue();

Keep in mind that this throws away the fraction though. If you want rounding instead:

int intResult = result.round(0, RoundingMode.HALF_UP).intValue();
🌐
Medium
solutionsarchitecture.medium.com › mastering-big-decimals-in-java-understanding-implementation-performance-and-alternatives-7e3a3a96efc6
Mastering Big Decimals in Java: Understanding Implementation, Performance, and Alternatives | by Rahul Krishnan | Medium
September 22, 2024 - How does it work?BigDecimal is implemented using a combination of integer arithmetic and scaling. The number is represented as an integer and a scale, which determines the number of digits to the right of the decimal point.
🌐
IONOS
ionos.com › digital guide › websites › web development › java class: bigdecimal
How to use Java BigDecimal to display floating point numbers
July 26, 2024 - You’ll also learn how to implement it for hashing and for precise, so­phis­ti­cat­ed com­par­isons. Java BigDec­i­mal consists of a 32-bit integer scale and an unscaled integer value with optional precision. In this case, “scale” means the number of digits after the decimal point, provided they’re greater than or equal to zero.
Find elsewhere
🌐
Codementor
codementor.io › community › bigdecimal class in java
BigDecimal Class in Java | Codementor
June 23, 2024 - According to its Javadoc, a BigDecimal represents an immutable, arbitrary-precision decimal number.
🌐
Medium
elvisciotti.medium.com › java-bigdecimal-in-javascript-9498fd4f2efe
Java BigDecimal in Javascript. To use Java’s BigDecimal operations… | by Elvis Ciotti | Medium
August 10, 2024 - For various numbers I tested from 0.00000154 to 4.67381e-10, those two lines of code return the same results. ... For numbers 649350.64935065, 0.0556933, 2139582054.04156352 multiplied by 0, or 0.00001 or 21228.58301, those two lines of code return identical values ... the only tiny difference I found is that Java BigDecimal adds trailing zeros (e.g.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - Converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal will be discarded.
🌐
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 - Most modern languages have dedicated libraries to handle decimals. Internally, they use either a long integer type or a string to store the number. They implement their own arithmetic engines. In Java, there is a BigDecimal class.
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
June 17, 2026 - Therefore, the value of the number represented by the BigDecimal is (Unscaled value × 10-Scale). The BigDecimal class in Java provides operations for basic arithmetic, scale manipulation, comparison, format conversion, and hashing.
🌐
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 - 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.
Author:
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - This method converts the double to its string representation before converting to BigDecimal, ensuring that we get the expected value every time. Just like the other Number classes (Integer, Long, Double, etc.), BigDecimal provides operations for arithmetic and comparison operations.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Java BigDecimal Example - Java Code Geeks
May 7, 2021 - Use pow(int n) method to get a BigDecimal whose value is the number raised to the n.
🌐
Daily.dev
app.daily.dev › home › collections › converting integer to bigdecimal in java
Converting Integer to BigDecimal in Java | daily.dev
January 15, 2024 - The static factory conversion method utilizes the valueOf method provided by the BigDecimal class. This method takes the Integer as a parameter and returns the equivalent BigDecimal object.
🌐
YouTube
youtube.com › watch
BigInteger & BigDecimal - Handling Large Numbers in Java - YouTube
In this tutorial, we will learn how to handle large numbers in Java using the BigInteger and BigDecimal classes.Take part in the learning in public initiativ...
Published: July 31, 2023
Top answer
1 of 8
185

The BigDecimal(double) constructor can have unpredictable behaviors. It is preferable to use BigDecimal(String) or BigDecimal.valueOf(double).

Use with AISystem.out.println(new BigDecimal(135.69)); //135.68999999999999772626324556767940521240234375
System.out.println(new BigDecimal("135.69")); // 135.69
System.out.println(new BigDecimal("13۵.۶9")); // 135.69
System.out.println(BigDecimal.valueOf(135.69)); // 135.69

The documentation for BigDecimal(double) explains in detail:

  1. 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.
  2. The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
  3. When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
2 of 8
41
Use with AIString currency = "135.69";
System.out.println(new BigDecimal(currency));

//will print 135.69