13

I want to create a method that calculates multiplication of an integer and a bigdecimal. I search on Google and forums, but I didn't find anything.

import java.math.BigDecimal;
private Integer quantite;
private BigDecimal prixUnit;

public Integer getQuantite() {
        return quantite;
}

public void setQuantite(Integer quantite) {
    this.quantite = quantite;
}


public BigDecimal getPrixUnit() {
    return prixUnit;
}

public void setPrixUnit(BigDecimal prixUnit) {
    this.prixUnit = prixUnit;
}

public BigDecimal methCal(BigDecimal quantite, BigDecimal prixUnit) {

    this.prixUnit=prixUnit;
    BigDecimal j = new BigDecimal(quantite);
    this.j = quantite;

    return quantite*prixUnit;
}

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ema
  • 175
  • 1
  • 2
  • 9

4 Answers4

29

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 );
}
Sheepy
  • 17,324
  • 4
  • 45
  • 69
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();
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
  • For a general purpose (floating-point values), I suggest you take a look at [Sheepy answer](https://stackoverflow.com/a/49853691/4391450). `valueOf` is more efficient for floating-point value. it is a better approach. – AxelH Apr 16 '18 at 10:21
  • @AxelH neither the question nor my answer deals with floating points as input though, and the closest relevant equivalent takes a long as parameter. That said, `valueOf` appears to yield cached values for values 0-10, which is more efficient than creating a new BigDecimal each time, so valueOf is indeed a better option. – Jeroen Steenbeeke Apr 16 '18 at 10:46
  • I said, for general purpose. It wasn't against your answer but to give more information in case of a floating point usage. – AxelH Apr 16 '18 at 10:48
1

Try this:

import java.math.*;

public class calculator {

    public static void main(String[] args) {

        BigDecimal value1 = new BigDecimal("3383878445");
        BigDecimal returnValue = calculation(2, value1);
        System.out.println("value is:" + returnValue);
    }

    public static BigDecimal calculation(int no1, BigDecimal no2) {
        BigDecimal value = BigDecimal.valueOf(no1).multiply(no2);
        return value;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MinA
  • 405
  • 4
  • 9
0

These methods from the Java API will be helpful.

public BigDecimal multiply​(BigDecimal multiplicand)

Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

Parameters: multiplicand - value to be multiplied by this BigDecimal. Returns: this * multiplicand

public BigDecimal​(int val)

Translates an int into a BigDecimal. The scale of the BigDecimal is zero.

Parameters: val - int value to be converted to BigDecimal.

Imal
  • 481
  • 1
  • 6
  • 14