Java BigDecimal subtract()方法及示例

Java BigDecimal subtract()方法及示例

java.math.BigDecimal .subtract(BigDecimal val)是用来计算两个BigDecimals的算术差。该方法用于寻找大数的算术差,而不影响结果的精度。该方法对当前的BigDecimal进行运算,该方法被调用时,BigDecimal作为参数被传递。

在java中,有两个减法的重载,如下所示。

  • subtract(BigDecimal val)
  • subtract (BigDecimal val, MathContext mc)

subtract(BigDecimal val)

语法

public BigDecimal subtract(BigDecimal val)

参数。本方法接受一个参数val,它是要从这个BigDecimal中减去的值。

返回值。该方法返回一个BigDecimal,该BigDecimal持有差值(this – val),其比例为max(this. scale() , val. scale() )。

下面的程序用来说明BigDecimal的减法()方法。

// Java program to demonstrate
// subtract() method of BigDecimal
  
import java.math.BigDecimal;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store result
        BigDecimal diff;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values to calculate the difference
        String input1
            = "545456468445645468464645";
        String input2
            = "425645648446468486486452";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal b
            = new BigDecimal(input2);
  
        // Using subtract() method
        diff = a.subtract(b);
  
        // Display the result in BigDecimal
        System.out.println("The difference of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + diff + "\n");
    }
}

输出。

The difference of
545456468445645468464645
and
425645648446468486486452
is
119810819999176981978193

subtract(BigDecimal val, MathContext mc)

语法

public BigDecimal subtract(BigDecimal val, MathContext mc)

参数。本方法接受两个参数,一个是val,是要从这个BigDecimal中减去的值,另一个是MathContext类型的mc。

返回值。该方法返回一个BigDecimal,其中包含差值(this – val),并根据上下文设置进行四舍五入。

下面的程序用来说明BigDecimal的减法()方法。

// Java program to demonstrate
// subtract() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store result
        BigDecimal diff;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values to calculate the difference
        String input1
            = "468445645468464645";
        String input2
            = "4256456484464684864864";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal b
            = new BigDecimal(input2);
  
        // Set precision to 10
        MathContext mc
            = new MathContext(10);
        // Using subtract() method
        diff = a.subtract(b, mc);
  
        // Display the result in BigDecimal
        System.out.println("The difference of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + diff + "\n");
    }
}

输出。

The difference of
468445645468464645
and
4256456484464684864864
is
-4.255988039E+21

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#subtract(java.math.BigDecimal)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程