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的更多相关文章
- (转)Java大数操作(BigInteger、BigDecimal)
基础知识 对于二进制来说,最高位代表正负号,-0表示-128,+0表示032位系统int型4个字节:-(2的31次方) ~ (2的31次方) 减 1最大负数:10000000 00000000 000 ...
- 关于Java大数操作(BigInteger、BigDecimal)
本文目标 可以使用BigInteger操作大整数 可以使用BigDecimal指定小数的保留位数 基础知识 对于二进制来说,最高位代表正负号,-0表示-128,+0表示032位系统int型4个字节:- ...
- java大数字操作:BigInteger,BigDecimal(浮点型)
java大数字操作: BigInteger:大数字整型的 BigDecimal(浮点型):大数字小数的,也适用大的整数 BigInteger: String num1 = "10038182 ...
- 用Java进行大数处理(BigInteger)-hdu1042
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目描述: 代码实现: import java.util.Scanner; import jav ...
- Java从零开始学二十九(大数操作(BigIntger、BigDecimal)
一.BigInteger 如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作. 不可变的任意精度的整数.所有操作中 ...
- java实现大数加法、乘法(BigDecimal)
之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...
- Java大数操作类
Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类. 大数操作类代码如下: packag ...
- Java 大数类BigInteger和BigDecimal的基本函数
在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运 ...
- 拯救你丢失的精度——BigInteger和BigDecimal类(入门)
第三阶段 JAVA常见对象的学习 BigInteger和BigDecimal类 BigInteger类 (一) 构造方法: //针对超过整数范围的运算(整数最大值:2147483647) BigInt ...
随机推荐
- BZOJ 1816 扑克牌
WA的我怀疑人生.. 发现原来是循环中间就要break掉,不然爆int. 总感觉这题可以直接构造啊.. #include<iostream> #include<cstdio> ...
- 查询所有表的记录数SQLServer
SELECT object_name (i.id) TableName, rows as RowCnt FROM sysindexes i INNER JOIN sysObj ...
- cmd命令进行RSA 密钥加密操作
--参考 http://msdn.microsoft.com/zh-cn/library/2w117ede http://msdn.microsoft.com/zh-cn/library/yxw286 ...
- latex 小结
1 no file .bbl http://blog.csdn.net/zhedasuiyuan/article/details/9223637 另外,你可能有多个 .bbl.目前的做法是,在控制台上 ...
- I.MX6 KEY_ROW4 can't as GPIO pin
/********************************************************************** * I.MX6 KEY_ROW4 can't as GP ...
- C语言字符串声明
重新学习C语言字符串声明char *a="nihao";char a[]="nihao";什么区别?前者定义的是指针,并且指向字符串常量“nihao”,后者是字 ...
- win7 64 下安装ubuntu14.04
win7下安装ubuntu方法: * 使用win7下的自带的分区工具给ubuntu留出磁盘空间:计算机 -> 右键菜单选择管理 -> 选择磁盘管理->选中最后的那个磁盘->右键 ...
- vs2012 + cocos2d-x 2.1.5 + win7开发环境搭建步骤
先要让vs具备cocos2d-x项目的模板,以此可以创建新的项目(1-5步),然后把相关的源码库文件和动态连接库都拷贝到自己的项目中,以使项目可以正常运行(6-7步). 1,打开vs,设置TestCp ...
- Redis的初步安装
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 下载 官网下载:http://redis.io/downlo ...
- 12. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...