简体   繁体   中英

How to multiply a BigInteger value by a BigDecimal value?

I have one BigInteger value and one BigDecimal value that I would like to multiply together and end up as a BigDecimal result.

For example if I have

BigInteger #1: 95

BigDecimal #1: 0.25124

Multiplying these together would give: 23.8678

Here is what I have tried:

    long firstLong = 95;
    float firstFloat = 0.25124f;
    BigInteger b1 = BigDecimal.valueOf(firstLong).toBigInteger();
    BigDecimal bd1 = BigDecimal.valueOf(firstFloat);

    BigDecimal multipliedResult = b1.multiply(bd1);
    System.out.println(multipliedResult);

Expected Output: 23.8678

Current Output: "The method multiply(BigInteger) in the type BigInteger is not applicable for the arguments (BigDecimal)"

Just turn things around:

BigDecimal multipliedResult = bd1.multiply(new BigDecimal(b1))

You can't do someBigInt.multiply(someBigDec) , but someBigDec.multiply(someBigDecCreatedFromBigInt) works nicely!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM