📜  Java中的 BigDecimal plus() 方法

📅  最后修改于: 2022-05-13 01:54:19.352000             🧑  作者: Mango

Java中的 BigDecimal plus() 方法

  1. Java.math.BigDecimal.plus()是Java中的一个内置方法,它返回一个 BigDecimal,其值为 (+this),其刻度为 this.scale()。为了与一元减法 negate() 对称,包含此方法,它仅返回此 BigDecimal。
    句法:
public BigDecimal plus()
  1. 参数:该函数不接受任何参数。
    返回值:该方法返回对象值即this。
    下面的程序说明了上述方法的工作:
    方案一:
Java
// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign value to b1
        BigDecimal b1 = new BigDecimal("-45.652");
 
        // Assign the result of plus method on
        // BigDecimal Objects b1 to b2
        BigDecimal b2 = b1.plus();
 
        // Print the value of b2
        System.out.println("The value of the BigDecimal is " + b2);
    }
}


Java
// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        // Assign value to b1
        BigDecimal b1 = new BigDecimal("7458.3256");
 
        // Assign the result of plus method on
        // BigDecimal Objects b1 to b2
        BigDecimal b2 = b1.plus();
 
        // Print the value of b2
        System.out.println("The value of the BigDecimal is " + b2);
    }
}


Java
// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-452.325");
 
        MathContext m = new MathContext(4); // 4 precision
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}


Java
// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-10.325");
 
        // 4 precision
        MathContext m = new MathContext(4);
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}


输出:
The value of the BigDecimal is -45.652

  1. 方案二:

Java

// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        // Assign value to b1
        BigDecimal b1 = new BigDecimal("7458.3256");
 
        // Assign the result of plus method on
        // BigDecimal Objects b1 to b2
        BigDecimal b2 = b1.plus();
 
        // Print the value of b2
        System.out.println("The value of the BigDecimal is " + b2);
    }
}
输出:
The value of the BigDecimal is 7458.3256

  1. Java.math.BigDecimal.plus(MathContext mc)是Java中的一个内置方法,它返回一个值为 (+this) 的 BigDecimal,并根据上下文设置进行舍入。
    句法:
public BigDecimal plus(MathContext mc)
  1. 参数:此方法接受单个参数mc ,它指的是要使用的舍入上下文,即,该值将被舍入到哪个数字。
    返回值:此方法返回 BigDecimal Object 的值,根据需要四舍五入。零结果的比例为 0。
    下面的程序说明了上述方法的工作:
    方案一:

Java

// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-452.325");
 
        MathContext m = new MathContext(4); // 4 precision
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}
输出:
Result of plus is -452.3

  1. 方案二:

Java

// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-10.325");
 
        // 4 precision
        MathContext m = new MathContext(4);
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}
输出:
Result of plus is -10.33

参考:https: Java()