BigInteger用于大整数运算
使用时需要导入包:import java.math.BigInteger;
初始化方法:
BigInteger a=new BigInteger(“123”); //第一种,参数是字符串
BigInteger a=BigInteger.valueOf(123); //第二种,参数可以是int、long
常用方法:
BigInteger abs() //返回大整数的绝对值
BigInteger add(BigInteger val) //返回两个大整数的和
BigInteger and(BigInteger val) //返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) //返回两个大整数与非的结果
BigInteger divide(BigInteger val) //返回两个大整数的商
double doubleValue() //返回大整数的double类型的值
float floatValue() //返回大整数的float类型的值
BigInteger gcd(BigInteger val) //返回大整数的最大公约数
int intValue()// 返回大整数的整型值
long longValue() //返回大整数的long型值
BigInteger max(BigInteger val)// 返回两个大整数的最大者
BigInteger min(BigInteger val)// 返回两个大整数的最小者
BigInteger mod(BigInteger val) //用当前大整数对val求模
BigInteger multiply(BigInteger val) //返回两个大整数的积
BigInteger negate() //返回当前大整数的相反数
BigInteger not() //返回当前大整数的非
BigInteger or(BigInteger val) //返回两个大整数的按位或
BigInteger pow(int exponent) //返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) //返回当前大整数除以val的余数
BigInteger leftShift(int n) //将当前大整数左移n位后返回
BigInteger rightShift(int n) //将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)//返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)//将大整数转换成二进制反码保存在byte数组中
BigInteger xor(BigInteger val) //返回两个大整数的异或
String toString() //将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) //返回两个大整数的异或
int a.compareTo(BigInteger b) //比较a,b的大小,如果a大于b则返回1,等于则返回0,小于则返回-1
代码示例:
import java.math.BigInteger; //引入math包中的BigInteger包
import java.util.*;
public class Java大数运算 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
//BigInteger大整数运算
BigInteger b1 = new BigInteger("1231243244124412"); //第一种初始化方法,参数是字符串
BigInteger b2 = BigInteger.valueOf(123124234); //第二种初始化方法,参数可以是int、long
System.out.println("加法操作:"+b2.add(b1)); //加法
System.out.println("减法操作:"+b2.subtract(b1)); //减法
System.out.println("乘法操作:"+b2.multiply(b1)); //乘法
System.out.println("除法操作:"+b2.divide(b1)); //除法
System.out.println("最大数:"+b2.max(b1));//最大值
System.out.println("最小数:"+b2.min(b1)); //最小值
BigInteger result[]=b2.divideAndRemainder(b1); //商和余数
System.out.println("商是:"+result[0]+" "+"余数是:"+result[1]);
}
}
执行结果:
BigDecimal用于小数的高精度运算
//最重要的方法
a.compareTo(b); //比较两个浮点数是否相等
代码示例:
import java.math.BigDecimal; //引入BigDecimal用于高精度运算
import java.util.*;
public class Java大数运算 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
//BigDecimal用于小数的高精度运算
System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345, 3.333), 1));
System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345, 3.333), 3));
System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345, 3.333), 4));
System.out.println("除法运算:" + MyMath.div(10.345, 3.333, 3));
}
}
class MyMath {
public static double add(double d1, double d2) { // 进行加法计算
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.add(b2).doubleValue();
}
public static double sub(double d1, double d2) { // 进行减法计算
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.subtract(b2).doubleValue();
}
public static double mul(double d1, double d2) { // 进行乘法计算
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.multiply(b2).doubleValue();
}
public static double div(double d1, double d2, int len) { // 进行除法计算
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double round(double d, int len) { // 进行四舍五入
BigDecimal b1 = new BigDecimal(d);
BigDecimal b2 = new BigDecimal(1); // 技巧
return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
执行结果: