bigdecimal乘法_Java BigDecimal乘法()方法与示例

bigdecimal乘法

BigDecimal类的multiple()方法 (BigDecimal Class multiply() method)

Syntax:

句法:

    public BigDecimal multiply(BigDecimal m_val);
    public BigDecimal multiply(BigDecimal m_val, MathContext ma_co);

  • multiply() method is available in java.math package.

    java.math包中提供了multiple ()方法

  • multiply(BigDecimal m_val) method is used get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal and its scale is calculated by using ([this BigDecimal.scale()] * [BigDecimal m_val.scale()]).

    使用乘法(BigDecimal m_val)方法来获取一个BigDecimal,该BigDecimal保留该BigDecimal乘以给定的BigDecimal的值,并使用([thisBigDecimal.scale()] * [BigDecimal m_val.scale()])计算其小数位数。

  • multiply(BigDecimal m_val, MathContext ma_co) method is used to get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal based on the given MathContext settings.

    乘法(BigDecimal m_val,MathContext ma_co)方法用于获取一个BigDecimal,该BigDecimal包含基于给定MathContext设置将此BigDecimal乘以给定BigDecimal的值。

  • These methods may throw an exception at the time of multiplied an object.

    这些方法在对象相乘时可能会引发异常。

    ArithmeticException: This exception may throw when the result is not accurate and set the rounding mode "UNNECESSARY".

    ArithmeticException :当结果不正确并且将舍入模式设置为“ UNNECESSARY”时,可能会引发此异常。

  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, multiply(BigDecimal m_val),

    在第一种情况下, 乘以(BigDecimal m_val)

    • BigDecimal m_val – represents the object is used to multiply this BigDecimal object.
    • BigDecimal m_val –表示用于将这个BigDecimal对象相乘的对象。
  • In the first case, multiply(BigDecimal m_val, MathContext ma_co),

    在第一种情况下, 乘法(BigDecimal m_val,MathContext ma_co)

    • BigDecimal m_val – Similar as defined in the first case.
    • BigDecimal m_val –与第一种情况下定义的相似。
    • MathContext ma_co – represents the context setting to use in rounding.
    • MathContext ma_co –表示要舍入的上下文设置。

Return value:

返回值:

In both the cases, the return type of the method is BigDecimal,

在这两种情况下,方法的返回类型均为BigDecimal 。

  • In the first case, it returns the multiplied result of both the objects without rounding.

    在第一种情况下,它返回两个对象的相乘结果而不进行舍入。

  • In the second case, it returns the multiplied result of both the objects with rounding if required.

    在第二种情况下,如果需要,它将使用舍入返回两个对象的相乘结果。

Example:

例:

// Java program to demonstrate the example 
// of multiply() method of BigDecimal

import java.math.*;

public class MultiplyOfBD {
    public static void main(String args[]) {
        // Initialize three variables val1,
        // val2 and mul_val 
        String val1 = "10.30";
        int val2 = 4;
        String mul_val = "5";

        // Initialize three BigDecimal objects and
        // one MathContext
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(val2);
        BigDecimal b_dec3 = new BigDecimal(mul_val);
        MathContext ma_co = new MathContext(2, RoundingMode.CEILING);

        System.out.println("multiply(BigDecimal): ");

        // multiplies this BigDecimal (b_dec2) by the
        // given BigDecimal (b_dec3) and return the result
        BigDecimal mul_res = b_dec2.multiply(b_dec3);
        System.out.println("b_dec2.multiply(b_dec3): " + mul_res);

        System.out.println(" ");

        System.out.println("multiply(BigDecimal,MathContext): ");

        // multiplies this BigDecimal (b_dec1) by the
        // given BigDecimal (b_dec3) and return the result based
        // on the given context settings
        mul_res = b_dec1.multiply(b_dec3, ma_co);
        System.out.println("b_dec1.multiply(b_dec3, ma_co): " + mul_res);
    }
}

Output

输出量

multiply(BigDecimal): 
b_dec2.multiply(b_dec3): 20
 
multiply(BigDecimal,MathContext): 
b_dec1.multiply(b_dec3, ma_co): 52


翻译自: https://www.includehelp.com/java/bigdecimal-multiply-method-with-example.aspx

bigdecimal乘法

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值