Math类、BigInteger大数类

本篇文章只是本人为方便查找相关方法而记录,下面具体代码并没有全部真正运行。

		***()Math数学工具类***
 		System.out.println("Math.E = " + Math.E);   //  指数
        System.out.println("Math.PI = " + Math.PI); //  圆周率
        // 包装器数据类型.MAX_VALUE 该类型所能表示的最大值  .MIN_VALUE 该类型所能表示的最小值
        //      用于判断溢出,比较最值
        System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
        System.out.println("Integer.MIN_VALUE = " + Integer.MIN_VALUE);
        int a=-1,b=2,c=3,d=4;
        double da=-1.2,db=2.4,dc=3.5,dd=4.8;
        System.out.println("Math.abs(a) = " + Math.abs(a));
        System.out.println("Math.max(a,b) = " + Math.max(a, b));
        System.out.println("Math.min(a,b) = " + Math.min(a, b));
        System.out.println("Math.pow(c,b) = " + Math.pow(c, b));
        System.out.println("Math.sqrt(d) = " + Math.sqrt(d));
        System.out.println("Math.log(Math.E) = " + Math.log(Math.E));
        System.out.println("Math.random() = " + Math.random());   // 产生一个随机小数
        System.out.println("Math.floor(db) = " + Math.floor(db));   // floor下取整
        System.out.println("Math.floor(dd) = " + Math.floor(dd));
        System.out.println("Math.ceil(db) = " + Math.ceil(db));     // ceil上取整
        System.out.println("Math.ceil(dd) = " + Math.ceil(dd));
        // 关注于小数点后第一位  <5符号不变,保留整数部分; =5 正数 整数部分+1,负数 不变
        //      >5 符号不变,整数部分+1
        System.out.println("Math.round(db) = " + Math.round(db));
        System.out.println("Math.round(dd) = " + Math.round(dd));
        // 四舍五入
        double f=111231.5585;
        BigDecimal bb=new BigDecimal(f);
        System.out.println(bb.setScale(2,  RoundingMode.HALF_UP).doubleValue());
        // 保留指定位数       #.后面几个0就代表了保留几位
        java.text.DecimalFormat df=new DecimalFormat("#.00");
        System.out.println("df.format(bb) = " + df.format(bb));
     
		***() BigInteger, BigDecimal大数类***
        //  (1) 新建对象 两种形式:参数为字符串/整型int,long等
        BigInteger a=new BigInteger("12");
        BigInteger a2=new BigInteger("FEDCBA",16); // 后面可以指明进制
        BigInteger b=BigInteger.valueOf(123);
        //  (2) 常用运算
        a.add(b);           //  加
        a.subtract(b);      //  减
        a.multiply(b);      //  乘
        a.divide(b);        //  除
        a.mod(b);           //  模
        a.max(b);           //  最大值
        a.min(b);           //  最小值
        a.gcd(b);           //  最大公约数
        a.abs();            //  绝对值
        a.and(b);           //  a&b
        a.andNot(b);        //  a&~b
        a.or(b);            //  a | b
        a.xor(b);           //  a ^ b
        //  (3) 转化为int, long, double类型
        a.intValue();
        a.longValue();
        a.doubleValue();
        //  (4) 大整数比较大小
        a.equals(b);        //  是否相等,相等为true,否则为false;
        a.compareTo(b);     //   a>b返回1,相等为0,a<b为-1
        //  (5) 一些常数
        // BigInteger.ZERO   0
        // BigInteger.ONE    1
        // BigInteger.TEN    10
        //  (6) 大整数的位数
        a.toString().length();
        //  (7) 转换为字符串
        a.toString();
        a.toString(8);   // 转化为指定进制下的字符串
        //  (8) 判断一个数是否为素数  正确率 1-1/2^certainty
        a.isProbablePrime(5);
		
		***() 进制转换***
		long ll=100;
        System.out.println("ll = " + ll);
        System.out.println("Long.toBinaryString(ll) = " + Long.toBinaryString(ll));  // 转换为二进制字符串
        System.out.println("Long.toOctalString(ll) = " + Long.toOctalString(ll));   // 八进制字符串
        System.out.println("Long.toHexString(ll) = " + Long.toHexString(ll));       //  十六进制字符串
        System.out.println("Long.toString(ll,6) = " + Long.toString(ll,6));  //  生成指定进制字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值