📜  用于 BigDecimal 和原始数据类型之间的算术运算的Java程序

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

用于 BigDecimal 和原始数据类型之间的算术运算的Java程序

在财务计算中使用的浮点数据类型(float 和 double)不太准确。因此, Java提供了一个单独的类“BigDecimal”来执行操作并避免计算中出现错误的最小机会。 BigDecimal 类为算术、比例处理、舍入、比较、格式转换和散列提供对双数的操作。它可以非常精确地处理非常大和非常小的浮点数,但稍微补偿了时间复杂度。 BigDecimal 提供了许多要实现的方法。在本文中,我们将讨论可以在 BigDecimal 上执行的基本算术运算,以及在 BigDecimal 和原始数据类型(如 int、short、long 等)之间执行相同的运算。

示例 1:

Java
// Java Program to Add, Subtract and Multiply
// Two big decimal numbers 
 
// Importing input output classes
import java.io.*;
 
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring two variables of type BigDecimal
        // Custom numbers for illustration
        BigDecimal num1 = new BigDecimal("15.3514628768");
        BigDecimal num2 = new BigDecimal("45.37844");
 
        // Mathematic operation over two numbers
 
        // Addition using add() method
        System.out.println("Addition of num1 and num2 = "
                           + (num1.add(num2)));
 
        // Subtraction using subtract() method
        System.out.println("Subtraction of num1 and num2 = "
                           + (num1.subtract(num2)));
 
        // Multiplication using multiply() method
        System.out.println(
            "Multiplication of num1 and num2 = "
            + (num1.multiply(num2)));
    }
}


Java
// Java Program to show exception thrown if blunt division
// of two big decimal numbers is carried on
 
// Importing input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
   
    // Main driver method
    public static void main (String[] args) {
       
        // Creating an object of VigDecimal class and
        // initializing the big decimal values
        // Custom entry 
        BigDecimal num1 = new BigDecimal("15.3514628768");
        BigDecimal num2 = new BigDecimal("45.37844");
       
        // Division over two numbers
        // using divide() method and printing the resultant number
        System.out.println("Division of num1 and num2 = " + (num2.divide(num1)));
    }
}


Java
// Java Program to show division of two big decimal numbers
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating and initializing double numbers
        // Custom entries
        double num1 = 15.3514628768;
        double num2 = 45.37844;
 
        // Dividing two big decimal numbers after which
        // numbers obtained will also be a big decimal
        // number
 
        // Print and display the resultant number
        System.out.println("Division of num1 and num2 = "
                           + (num2 / num1));
    }
}


Java
// Java Program to Add big decimal number
// with an Integer number
 
// Importing input output classes
import java.io.*;
 
// importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Adding a big decimal number with
        // an integer number
        System.out.println("Addition of num1 and num2 ="
                           + num1.add(num2));
    }
}


Java
// Java Program to Add big decimal number
// with an Integer number
 
// Importing input output classes
import java.io.*;
 
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Print the addition of two numbers
        System.out.println(
            "Addition of num1 and num2 = "
            + num1.add(new BigDecimal(num2)));
    }
}


Java
// Importing all input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Addition over numbers
        // Print the resultant desired number
        System.out.println(
            "Addition of num1 and num2 = "
            + num1.add(new BigDecimal(num2)));
 
        // Subtraction over numbers
        // Print the resultant desired number
        System.out.println(
            "Subtraction of num1 and num2 = "
            + num1.subtract(new BigDecimal(num2)));
 
        // Multiplication over numbers
        // Print the resultant desired number
        System.out.println(
            "Multiplication of num1 and num2 = "
            + num1.multiply(new BigDecimal(num2)));
 
        // Division over numbers
        // Print the resultant desired number
        System.out.println(
            "Division of num1 and num2 = "
            + num1.divide(new BigDecimal(num2)));
    }
}


输出
Addition of num1 and num2 = 60.7299028768
Subtraction of num1 and num2 = -30.0269771232
Multiplication of num1 and num2 = 696.625437067096192

输出说明:

请注意,操作的准确性非常高。现在对以下示例中的同一组数字执行除法,如下所示:



示例 2:

Java

// Java Program to show exception thrown if blunt division
// of two big decimal numbers is carried on
 
// Importing input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
   
    // Main driver method
    public static void main (String[] args) {
       
        // Creating an object of VigDecimal class and
        // initializing the big decimal values
        // Custom entry 
        BigDecimal num1 = new BigDecimal("15.3514628768");
        BigDecimal num2 = new BigDecimal("45.37844");
       
        // Division over two numbers
        // using divide() method and printing the resultant number
        System.out.println("Division of num1 and num2 = " + (num2.divide(num1)));
    }
}

输出:

输出说明:

出现这个错误是因为两个数字的除法是非终止的,我们知道引入 BigDecimal 是为了提供最大的精度。因此,它会产生错误。我们将在下一个代码中纠正相同的问题,我们将除以相同的数字,但现在数据类型是 double,它应该不会产生任何错误并给出一些答案。

示例 3:



Java

// Java Program to show division of two big decimal numbers
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating and initializing double numbers
        // Custom entries
        double num1 = 15.3514628768;
        double num2 = 45.37844;
 
        // Dividing two big decimal numbers after which
        // numbers obtained will also be a big decimal
        // number
 
        // Print and display the resultant number
        System.out.println("Division of num1 and num2 = "
                           + (num2 / num1));
    }
}
输出
Division of num1 and num2 = 2.955968454874647

示例 4:

Java

// Java Program to Add big decimal number
// with an Integer number
 
// Importing input output classes
import java.io.*;
 
// importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Adding a big decimal number with
        // an integer number
        System.out.println("Addition of num1 and num2 ="
                           + num1.add(num2));
    }
}


输出:

输出说明:

这是因为 BigDecimal 只允许对 BigDecimal 对象执行操作。因此,我们需要使用 BigDecimal Class 的构造函数将我们的原始数据类型变量转换为 BigDecimal 的对象。上面的冲突在下面的代码中解决如下。

示例 5:

Java

// Java Program to Add big decimal number
// with an Integer number
 
// Importing input output classes
import java.io.*;
 
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Print the addition of two numbers
        System.out.println(
            "Addition of num1 and num2 = "
            + num1.add(new BigDecimal(num2)));
    }
}
输出
Addition of num1 and num2 = 27

输出说明:

这里发生的事情是我们正在创建一个类 BigDecimal 的新对象,其值与 num2 相同,我们直接将通过构造函数创建的对象传递给 add() 方法的参数。我们得到了所需的答案。 BigDecimal 和 int 之间的算术运算如下所示。

示例 6:

Java

// Importing all input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring a BigDecimal object
        BigDecimal num1 = new BigDecimal("12");
 
        // Declaring an integer number
        int num2 = 15;
 
        // Addition over numbers
        // Print the resultant desired number
        System.out.println(
            "Addition of num1 and num2 = "
            + num1.add(new BigDecimal(num2)));
 
        // Subtraction over numbers
        // Print the resultant desired number
        System.out.println(
            "Subtraction of num1 and num2 = "
            + num1.subtract(new BigDecimal(num2)));
 
        // Multiplication over numbers
        // Print the resultant desired number
        System.out.println(
            "Multiplication of num1 and num2 = "
            + num1.multiply(new BigDecimal(num2)));
 
        // Division over numbers
        // Print the resultant desired number
        System.out.println(
            "Division of num1 and num2 = "
            + num1.divide(new BigDecimal(num2)));
    }
}
输出
Addition of num1 and num2 = 27
Subtraction of num1 and num2 = -3
Multiplication of num1 and num2 = 180
Division of num1 and num2 = 0.8