BigInteger的理解
-
BigInteger类型的数字范围较 Integer 类型的数字范围要大得多。
-
我们都知道 Integer 是 Int 的包装类,int 的最大值为 231-1,如果要计算更大的数字,使用Integer 数据类型就无法实现了,所以 Java 中提供了BigInteger 类来处理更大的数字。
-
BigInteger 支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。
-
在 BigInteger 类中封装了多种操作!除了基本的加减乘除操作之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。
-
使用BigInteger 类,可以实例化一个BigInteger 对象,并自动调用相应的构造函数。
-
BigInteger 类具有很多构造函数,但最直接的一种方式是参数以字符串形式代表要处理的数字。
public BigInteger(String val) 其中,val 是十进制字符串。 如果将 2 转换为 BigInteger 类型,可以 使用以下语句进行初始化操作: BigInteger twoInstance = new BigInteger ("2");
-
BigInteger 类几种常用的运算方法
public BigInteger add(BigInteger val):做加法运算 public BigInteger subtract(BigInteger val):做减法运算 public BigInteger multiply(BigInteger val):做乘法运算 public BigInteger divide(BigInteger val):做除法运算 public BigInteger remainder(BigInteger val):做取余操作 public BigInteger pow(int exponet):进行取参数的 exponet 次方操作 public BigInteger negate():取相反数 public BigInteger shiftLegt(int n):将数字左移 n 位,如果 n 为负数,做右移操作 public BigInteger shiftRight(int n):将数字右移 n 位,如果 n 为负数,做左移操作 public int compareTo(BigInteger val):做数字比较操作 public BigInteger max(BigInteger val):返回较大的数值
-
实例
## BigInteger的理解 public class BigIntegerDemo { public static void main(String[] args) { BigInteger bigInstance = new BigInteger("4"); //实例化一个大数字 //取该大数字加2的操作 System.**out**.println("加法操作:"+bigInstance.add(**new** BigInteger("2"))); //取该大数字减2的操作 System.**out**.println("减法操作:"+bigInstance.subtract(**new** BigInteger("2"))); //取该大数字乘以2的操作 System.**out**.println("乘法操作:"+bigInstance.multiply(**new** BigInteger("2"))); //取该大数字除以2的操作 System.**out**.println("除法操作:"+bigInstance.divide(**new** BigInteger("2"))); //取该大数字除以3的商 System.**out**.println("取商:"+bigInstance.divideAndRemainder(**new** BigInteger("3"))[0]); //取该大数字除以3的余数 System.**out**.println("取余数:"+ bigInstance.divideAndRemainder(**new** BigInteger("3"))[1]); //取该大数字的2次方 System.**out**.println("做2次方操作:"+ bigInstance.pow(2)); //取该大数字的相反数 System.**out**.println("取相反数操作:"+bigInstance.negate()); } }
BigDecimal的理解
-
针对大的小数的处理类
-
不推荐使用BigDecimal(double val)构造器,因为使用该构造器时有一定的不可预知性,
-
当程序使用new BigDecimal(0.1)创建一个BigDecimal对象时,它的值并不是0.1,实际上是一个近似0.1的数。
-
建议优先使用基于String的构造器,使用BigDecimal(String val)构造器时可以预知的,写入new BigDecimal(“0.1”)将创建一个恰好等于0.1的BigDecimal。
-
如果必须使用double浮点数作为BigDecimal构造器的参数时,不要使用double作为参数,而应该通过BigDecimal.valueOf(double value)静态方法来创建对象。
-
实例
public class BigDecimalDemo { // 除法运算默认精度 private static final int DEF_DIV_SCALE = 10; /** * 精确加法 */ public static double add(double value1, double value2) { BigDecimal b1 = BigDecimal.*valueOf*(value1); BigDecimal b2 = BigDecimal.*valueOf*(value2); return b1.add(b2).doubleValue(); } /** * 精确减法 */ public static double sub(double value1, double value2) { BigDecimal b1 = BigDecimal.*valueOf*(value1); BigDecimal b2 = BigDecimal.*valueOf*(value2); return b1.subtract(b2).doubleValue(); } /** * 精确乘法 */ public static double mul(double value1, double value2) { BigDecimal b1 = BigDecimal.*valueOf*(value1); BigDecimal b2 = BigDecimal.*valueOf*(value2); return b1.multiply(b2).doubleValue(); } /** * 精确除法 使用默认精度 */ public static double div(double value1, double value2) throws IllegalAccessException { return *div*(value1, value2, DEF_DIV_SCALE); } /** * 精确除法 * @param scale 精度 */ public static double div(double value1, double value2, int scale) throws IllegalAccessException { if(scale < 0) { throw new IllegalAccessException("精确度不能小于0"); } BigDecimal b1 = BigDecimal.*valueOf*(value1); BigDecimal b2 = BigDecimal.*valueOf*(value2); // return b1.divide(b2, scale).doubleValue(); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * 四舍五入 * @param scale 小数点后保留几位 */ public static double round(double v, int scale) throws IllegalAccessException { return *div*(v, 1, scale); } /** * 比较大小 */ public static boolean equalTo(BigDecimal b1, BigDecimal b2) { if(b1 == null || b2 == null) { return false; } return 0 == b1.compareTo(b2); } public static void main(String[] args) throws IllegalAccessException { double value1=1.2345678912311; double value2=9.1234567890123; BigDecimal value3=new BigDecimal(Double.*toString*(value1)); BigDecimal value4=new BigDecimal(Double.*toString*(value2)); System.out.println("精确加法================="+BigDecimalDemo.*add*(value1, value2)); System.out.println("精确减法================="+BigDecimalDemo.*sub*(value1, value2)); System.out.println("精确乘法================="+BigDecimalDemo.*mul*(value1, value2)); System.out.println("精确除法 使用默认精度 ================="+BigDecimalDemo.*div*(value1, value2)); System.out.println("精确除法 设置精度================="+BigDecimalDemo.*div*(value1, value2,20)); System.out.println("四舍五入 小数点后保留几位 ================="+BigDecimalDemo.*round*(value1, 10)); System.out.println("比较大小 ================="+BigDecimalDemo.*equalTo*(value3, value4)); } }