Java的大数操作分为BigInteger和BigDecimal

Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类。

大数操作类代码如下:

  1 package blog;
  2
  3 import java.math.BigDecimal;
  4 import java.math.BigInteger;
  5 import java.math.RoundingMode;
  6
  7 /**
  8  *
  9  * @author 瓦尔登湖畔的小木屋
 10  * BigNumberOperation封装了对十进制大数整数和大数浮点数的四则运算的操作
 11  */
 12 public class BigNumberOperation {
 13     /**
 14      * 对加法的封装操作
 15      */
 16     public static BigInteger add(BigInteger number1, BigInteger number2) {
 17         return number1.add(number2);
 18     }
 19
 20     public static BigDecimal add(BigDecimal number1, BigDecimal number2) {
 21         return number1.add(number2);
 22     }
 23
 24     public static BigDecimal add(BigDecimal number1, BigInteger number2) {
 25         return number1.add(new BigDecimal(number2));
 26     }
 27
 28     public static BigDecimal add(BigInteger number1, BigDecimal number2) {
 29         return new BigDecimal(number1).add(number2);
 30     }
 31
 32     /**
 33      * 对减法的封装操作
 34      */
 35     public static BigInteger subtract(BigInteger number1, BigInteger number2) {
 36         return number1.subtract(number2);
 37     }
 38
 39     public static BigDecimal subtract(BigDecimal number1, BigDecimal number2) {
 40         return number1.subtract(number2);
 41     }
 42
 43     public static BigDecimal subtract(BigDecimal number1, BigInteger number2) {
 44         return number1.subtract(new BigDecimal(number2));
 45     }
 46
 47     public static BigDecimal subtract(BigInteger number1, BigDecimal number2) {
 48         return new BigDecimal(number1).subtract(number2);
 49     }
 50
 51     /**
 52      * 对乘法的封装操作
 53      */
 54     public static BigInteger multiply(BigInteger number1, BigInteger number2) {
 55         return number1.multiply(number2);
 56     }
 57
 58     public static BigDecimal multiply(BigDecimal number1, BigDecimal number2) {
 59         return number1.multiply(number2);
 60     }
 61
 62     public static BigDecimal multiply(BigDecimal number1, BigInteger number2) {
 63         return number1.multiply(new BigDecimal(number2));
 64     }
 65
 66     public static BigDecimal multiply(BigInteger number1, BigDecimal number2) {
 67         return new BigDecimal(number1).multiply(number2);
 68     }
 69
 70     /**
 71      * 对除法的封装
 72      * 大数浮点数时,结果默认保留10位小数,也可以通过count指定保留小数的位数
 73      * 对于最末一位的取舍采取RoundingMode.HALF_EVEN模式
 74      */
 75     public static BigInteger divide(BigInteger number1, BigInteger number2) {
 76         return number1.divide(number2);
 77     }
 78
 79     public static BigDecimal divide(BigDecimal number1, BigDecimal number2) {
 80         return number1.divide(number2, 10, RoundingMode.HALF_EVEN);
 81     }
 82
 83     public static BigDecimal divide(BigDecimal number1, BigDecimal number2, int count) {
 84         return number1.divide(number2, count, RoundingMode.HALF_EVEN);
 85     }
 86
 87     public static BigDecimal divide(BigDecimal number1, BigInteger number2) {
 88         return number1.divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN);
 89     }
 90
 91     public static BigDecimal divide(BigDecimal number1, BigInteger number2, int count) {
 92         return number1.divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN);
 93     }
 94
 95     public static BigDecimal divide(BigInteger number1, BigDecimal number2) {
 96         return new BigDecimal(number1).divide(number2, 10, RoundingMode.HALF_EVEN);
 97     }
 98
 99     public static BigDecimal divide(BigInteger number1, BigDecimal number2, int count) {
100         return new BigDecimal(number1).divide(number2, count, RoundingMode.HALF_EVEN);
101     }
102     /*
103      * 由于当两个大数都是字符串时,无法通过重载实现,所以才返回Number,以同时适应BigInteger和BigDecimal
104      */
105     public static Number add(String number1, String number2) {
106         if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
107             return new BigInteger(number1).add(new BigInteger(number2));
108         } else {
109             return new BigDecimal(number1).add(new BigDecimal(number2));
110         }
111     }
112
113     public static Number subtract(String number1, String number2) {
114         if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
115             return new BigInteger(number1).subtract(new BigInteger(number2));
116         } else {
117             return new BigDecimal(number1).subtract(new BigDecimal(number2));
118         }
119     }
120
121     public static Number multiply(String number1, String number2) {
122         if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
123             return new BigInteger(number1).multiply(new BigInteger(number2));
124         } else {
125             return new BigDecimal(number1).multiply(new BigDecimal(number2));
126         }
127     }
128
129     public static Number divide(String number1, String number2) {
130         if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
131             return new BigInteger(number1).divide(new BigInteger(number2));
132         } else {
133             return new BigDecimal(number1).divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN);
134         }
135     }
136
137     public static BigDecimal divide(String number1, String number2, int count) {
138         return new BigDecimal(number1).divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN);
139     }
140 }

测试代码如下:

 1 package blog;
 2
 3 import java.math.BigDecimal;
 4 import java.math.BigInteger;
 5
 6 public class Main {
 7     public static void main(String[] args) {
 8         String str1 = "46556545";
 9         String str2 = "45454545.45454544";
10
11         BigInteger num1 = new BigInteger(str1);
12         BigDecimal num2 = new BigDecimal(str2);
13         System.out.println("当两个参数是大数时的测试:");
14         System.out.println("大数加法的测试:");
15         System.out.println(num1 + " + " + num1 + " = " + BigNumberOperation.add(num1, num1));
16         System.out.println(num1 + " + " + num2 + " = " + BigNumberOperation.add(num1, num2));
17         System.out.println(num2 + " + " + num1 + " = " + BigNumberOperation.add(num2, num1));
18         System.out.println(num2 + " + " + num2 + " = " + BigNumberOperation.add(num2, num2));
19         System.out.println();
20
21         System.out.println("大数减法的测试:");
22         System.out.println(num1 + " - " + num1 + " = " + BigNumberOperation.subtract(num1, num1));
23         System.out.println(num1 + " - " + num2 + " = " + BigNumberOperation.subtract(num1, num2));
24         System.out.println(num2 + " - " + num1 + " = " + BigNumberOperation.subtract(num2, num1));
25         System.out.println(num2 + " - " + num2 + " = " + BigNumberOperation.subtract(num2, num2));
26         System.out.println();
27
28         System.out.println("大数乘法的测试:");
29         System.out.println(num1 + " * " + num1 + " = " + BigNumberOperation.multiply(num1, num1));
30         System.out.println(num1 + " * " + num2 + " = " + BigNumberOperation.multiply(num1, num2));
31         System.out.println(num2 + " * " + num1 + " = " + BigNumberOperation.multiply(num2, num1));
32         System.out.println(num2 + " * " + num2 + " = " + BigNumberOperation.multiply(num2, num2));
33         System.out.println();
34
35         System.out.println("大数除法的测试:");
36         System.out.println(num1 + " / " + num1 + " = " + BigNumberOperation.divide(num1, num1));
37         System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2));
38         System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1));
39         System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2));
40         System.out.println("大数除法保留五位小数:");
41         System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2, 5));
42         System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1, 5));
43         System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2, 5));
44         System.out.println("");
45
46         System.out.println("###############################################");
47         System.out.println("当两个参数为字符串是的测试:");
48         System.out.println("加法测试:");
49         System.out.println(str1 + " + " + str1 + " = " + BigNumberOperation.add(str1, str1));
50         System.out.println(str1 + " + " + str2 + " = " + BigNumberOperation.add(str1, str2));
51         System.out.println(str2 + " + " + str1 + " = " + BigNumberOperation.add(str2, str1));
52         System.out.println(str2 + " + " + str2 + " = " + BigNumberOperation.add(str2, str2));
53         System.out.println();
54         System.out.println("减法测试:");
55         System.out.println(str1 + " - " + str1 + " = " + BigNumberOperation.subtract(str1, str1));
56         System.out.println(str1 + " - " + str2 + " = " + BigNumberOperation.subtract(str1, str2));
57         System.out.println(str2 + " - " + str1 + " = " + BigNumberOperation.subtract(str2, str1));
58         System.out.println(str2 + " - " + str2 + " = " + BigNumberOperation.subtract(str2, str2));
59         System.out.println();
60         System.out.println("乘法测试:");
61         System.out.println(str1 + " * " + str1 + " = " + BigNumberOperation.multiply(str1, str1));
62         System.out.println(str1 + " * " + str2 + " = " + BigNumberOperation.multiply(str1, str2));
63         System.out.println(str2 + " * " + str1 + " = " + BigNumberOperation.multiply(str2, str1));
64         System.out.println(str2 + " * " + str2 + " = " + BigNumberOperation.multiply(str2, str2));
65         System.out.println();
66         System.out.println("除法测试:");
67         System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1));
68         System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2));
69         System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1));
70         System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2));
71         System.out.println();
72         System.out.println("除法测试:");
73         System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1, 5));
74         System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2, 5));
75         System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1, 5));
76         System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2, 5));
77     }
78 }

测试结果如下:

当两个参数是大数时的测试:
大数加法的测试:
46556545 + 46556545 = 93113090
46556545 + 45454545.45454544 = 92011090.45454544
45454545.45454544 + 46556545 = 92011090.45454544
45454545.45454544 + 45454545.45454544 = 90909090.90909088

大数减法的测试:
46556545 - 46556545 = 0
46556545 - 45454545.45454544 = 1101999.54545456
45454545.45454544 - 46556545 = -1101999.54545456
45454545.45454544 - 45454545.45454544 = 0E-8

大数乘法的测试:
46556545 * 46556545 = 2167511882337025
46556545 * 45454545.45454544 = 2116206590909090.23190480
45454545.45454544 * 46556545 = 2116206590909090.23190480
45454545.45454544 * 45454545.45454544 = 2066115702479337.5206611570247936

大数除法的测试:
46556545 / 46556545 = 1
46556545 / 45454545.45454544 = 1.0242439900
45454545.45454544 / 46556545 = 0.9763298684
45454545.45454544 / 45454545.45454544 = 1.0000000000
大数除法保留五位小数:
46556545 / 45454545.45454544 = 1.02424
45454545.45454544 / 46556545 = 0.97633
45454545.45454544 / 45454545.45454544 = 1.00000

###############################################
当两个参数为字符串是的测试:
加法测试:
46556545 + 46556545 = 93113090
46556545 + 45454545.45454544 = 92011090.45454544
45454545.45454544 + 46556545 = 92011090.45454544
45454545.45454544 + 45454545.45454544 = 90909090.90909088

减法测试:
46556545 - 46556545 = 0
46556545 - 45454545.45454544 = 1101999.54545456
45454545.45454544 - 46556545 = -1101999.54545456
45454545.45454544 - 45454545.45454544 = 0E-8

乘法测试:
46556545 * 46556545 = 2167511882337025
46556545 * 45454545.45454544 = 2116206590909090.23190480
45454545.45454544 * 46556545 = 2116206590909090.23190480
45454545.45454544 * 45454545.45454544 = 2066115702479337.5206611570247936

除法测试:
46556545 / 46556545 = 1
46556545 / 45454545.45454544 = 1.0242439900
45454545.45454544 / 46556545 = 0.9763298684
45454545.45454544 / 45454545.45454544 = 1.0000000000

除法测试:
46556545 / 46556545 = 1.00000
46556545 / 45454545.45454544 = 1.02424
45454545.45454544 / 46556545 = 0.97633
45454545.45454544 / 45454545.45454544 = 1.00000

Java的大数操作分为BigInteger和BigDecimal

时间: 2024-10-04 14:19:31

Java的大数操作分为BigInteger和BigDecimal的相关文章

java大数字操作:BigInteger,BigDecimal(浮点型)

java大数字操作: BigInteger:大数字整型的 BigDecimal(浮点型):大数字小数的,也适用大的整数 BigInteger: String num1 = "100381828646148164"; String num2 = "10998979766868"; BigInteger big1 = new BigInteger(num1); BigInteger big2 =new BigInteger(num2); System.out.print

Java从零开始学二十九(大数操作(BigIntger、BigDecimal)

一.BigInteger 如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作. 不可变的任意精度的整数.所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型).BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法.另外,BigInteger 还提供以下运算:模算术.GCD 计算.质数测试.素数生成.位操作以及一

Java 大数类BigInteger和BigDecimal的基本函数

在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运算变得很简单,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大. 这两个类都在java.math.*包中,因此每次必须在开头处引用该包. Ⅰ基本函数: 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger

Java基础之BigInteger,BigDecimal

JAVA的两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,理论上能够表示无限大的数. 1:大整数:BigInteger import java.util.*; import java.math.*; public class Test { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //BigInteger类型的常量 BigInteger A = Big

Java基本的程序结构设计 大数操作

大数操作 BigInteger 不可变的任意精度的整数.所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型).BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法.另外,BigInteger 还提供以下运算:模算术.GCD 计算.质数测试.素数生成.位操作以及一些其他操作. 个人理解:可以理解为BigInteger内部维护了一个int数组,这个数组可以无限大.java在对BigInteger进

Java中的大数处理类BigInteger和BigDecimar浅析

这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math.BigInteger;和import java.math.BigDecimal; BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运算变得很简单. 下面从几个方面对BigInteger和BigDecima做一个简单的比较: 一.常量 BigInteger:ONE,ZE

java常用类详细介绍及总结:字符串相关类、日期时间API、比较器接口、System、Math、BigInteger与BigDecimal

一.字符串相关的类 1.String及常用方法 1.1 String的特性 String:字符串,使用一对""引起来表示. String声明为final的,不可被继承 String实现了Serializable接口:表示字符串是支持序列化的. 实现了Comparable接口:表示String可以比较大小 String内部定义了final char[] value用于存储字符串数据 String:代表不可变的字符序列.简称:不可变性. 体现: 当对字符串重新赋值时,需要重写指定内存区域赋

JAVA求解大数

题目链接:CLICK HERE~ 有了Java求解大数变得如此简单,以后再也不用担心大数模板了.哦啦啦啦. import java.math.BigInteger; import java.math.BigDecimal; import java.util.Scanner; class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); while(cin.hasNext()){

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[