You would call myBigDecimal.intValueExact() (or just intValue()) and it will even throw an exception if you would lose information. That returns an int but autoboxing takes care of that.

Answer from willcodejavaforfood on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal intvalue in java
BigDecimal intValue in Java
September 1, 2008 - The java.math.BigDecimal.intValue()converts this BigDecimal to an int.
🌐
Blogger
javahungry.blogspot.com › 2022 › 03 › bigdecimal-to-integer.html
Convert BigDecimal to Integer in Java [3 ways] | Java Hungry
You can easily use BigDecimal class intValueExact() method to convert BigDecimal to Integer in Java.
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();
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - NumberFormatException - if val is not a valid representation of a BigDecimal. ... Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-intvalue-method-in-java
BigDecimal intValue() Method in Java - GeeksforGeeks
December 4, 2018 - The java.math.BigDecimal.intValue() is an in-built function which converts this BigDecimal to an integer value. This function discards any fractional part of this BigDecimal. If the result of the conversion is too big to be represented as an ...
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_tobiginteger.htm
Java.math.BigDecimal.toBigInteger() Method
The java.math.BigDecimal.toBigInteger() converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long. Any fractional part of this BigDecimal will be discarded. This conversion can lose information about the precision of the BigDecim
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - It’s particularly useful in situations where we need to perform calculations that require accuracy, such as financial transactions. BigDecimal represents an immutable arbitrary-precision signed decimal number. It consists of two parts: Unscaled value: This is an integer that represents the number without any decimal point
🌐
Stack Overflow
stackoverflow.com › questions › 30923649 › issue-while-converting-bigdecimal-to-integer
java - issue while converting BigDecimal to Integer - Stack Overflow
What I mean to say is do this: int myInt = myBigDecimal.intValue(); This will get you the int value of your BigDecimal. For more info, go to docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html 2015-06-18T19:23:08.34Z+00:00 ... That too i tried.. But couldn't because the object itself is list of Integers.
🌐
Oracle
docs.oracle.com › cd › E13222_01 › wls › docs45 › classdocs › java.math.BigDecimal.html
Class java.math.BigDecimal
Any extraneous characters (including whitespace) will result in a NumberFormatException. BigDecimal · public BigDecimal(double val) throws NumberFormatException · Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10**scale * val) is an integer.
🌐
Talend
community.talend.com › s › question › 0D53p00007vCntMCAS › bigdecimal-to-integer-conversion
BigDecimal to integer conversion
We have a task that is replicating data to Kafka using a Microsoft SQL Server source (reading from a Log Stream file, which was populated by a Microsoft SQL Server task, not CDC). We want to apply a global rule that filters out all changes committed by a specific user.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
NumberFormatException - if val is not a valid representation of a BigDecimal. ... Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-tobiginteger-method-in-java
BigDecimal toBigInteger() Method in Java - GeeksforGeeks
November 9, 2021 - The java.math.BigDecimal.toBigInteger() is an inbuilt method in java that converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long.
🌐
Blogger
javahungry.blogspot.com › 2021 › 06 › integer-to-bigdecimal.html
Convert Integer to BigDecimal in Java [2 ways] | Java Hungry
BigDecimal.valueOf(int); Below is the complete example to convert Integer and primitive int to BigDecimal in Java using the valueOf() method.
🌐
Coderanch
coderanch.com › t › 557228 › java › Divide-BigDecimal-Integer
Divide BigDecimal by Integer (Java in General forum at Coderanch)
Winston Gutkowski wrote: There's nothing intrinsically wrong with it, but the redundant double conversion will slow things down. varBigDecimal1.divide(BigDecimal.valueOf(varInteger1))will work just as well. Winston · More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason...including blind stupidity. Nothing wrong with the advice (it's probably sound) but yet whenever I see someone take about efficiency I love to use that quote ....
🌐
Baeldung
baeldung.com › home › java › java numbers › multiply a bigdecimal by an integer in java
Multiply a BigDecimal by an Integer in Java | Baeldung
January 8, 2024 - An example can explain the problem quickly. Let’s say we have a BigDecimal number and an integer: final BigDecimal BIG = new BigDecimal("42.42"); final int INT = 10; Then we want to calculate the result of 42.42 x 10 as another BigDecimal number:
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.intvalue
BigDecimal.IntValue Method (Java.Math) | Microsoft Learn
This conversion is analogous to the narrowing primitive conversion from double to short as defined in <cite>The Java Language Specification</cite>: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned.
🌐
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.