代码改变世界

math类和biginteger类

2018-05-30 16:49  yelena  阅读(183)  评论(0编辑  收藏  举报

Math类:这种工具类,一般不会创建对象,方法为静态方法,直接调用

package com.oracle.demo02;

public class MathDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //绝对值
        System.out.println(Math.abs(-99));
        //向上取整
        System.out.println(Math.ceil(12.2));
        //向下取整
        System.out.println(Math.floor(12.2));
        //取最大值
        System.out.println(Math.max(111,333));
        //取最小值
        System.out.println(Math.min(111,333));
        //取次幂
        System.out.println(Math.pow(2,3));
        //取随机数
        System.out.println(Math.random());
        //四舍五入
        System.out.println(Math.round(12.2));
        //π
        System.out.println(Math.PI);
    }

}

Array类:操作数组的一些方法

package com.oracle.demo02;

import java.util.Arrays;

public class ArraysDemo {
    public static void main(String[] args) {
        method1();
        
        
    }
    public static void method(){
        int[] arr = {3,6,9,11,15};
        //返回指定值对应的索引值(数组必须有序)
        //如果不存在,索引值=(-当前索引-1)
        int i = Arrays.binarySearch(arr, 6);
        System.out.println(i);
    }
    public static void method1(){
        int[] arr = {5,7,1,3,2,9,};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
//        for(int i:arr){
//            System.out.print(i);
//        }
    }
}

BigInteger类:比long类型的数据还大的值

package com.oracle.demo02;

import java.math.BigInteger;

public class BigDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        method();
    }
    public static void method(){
        BigInteger bi =new BigInteger("100000000000000000000000000000000000000000");
        BigInteger bi2 = new BigInteger("20000000000000000000000000000000000000000");
        //加法
        System.out.println(bi.add(bi2));
        //减法
        System.out.println(bi.subtract(bi2));
        //乘法
        System.out.println(bi.multiply(bi2));
        //除法
        System.out.println(bi.divide(bi2));
    }
}

BigDecimal类:避免精度损失的

package com.oracle.demo02;

import java.math.BigDecimal;

public class BigDemo01 {
    public static void main(String[] args) {
        method3();
    }
    public static void method(){
        BigDecimal bd1 = new BigDecimal("0.09");
        BigDecimal bd2 = new BigDecimal("0.01");
        System.out.println(bd1.add(bd2));
    }
    public static void method1(){
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.31");
        System.out.println(bd1.subtract(bd2));
    }
    public static void method2(){
        BigDecimal bd1 = new BigDecimal("1.015");
        BigDecimal bd2 = new BigDecimal("100");
        System.out.println(bd1.multiply(bd2));
    }
    public static void method3(){
        BigDecimal bd1 = new BigDecimal("1.301");
        BigDecimal bd2 = new BigDecimal("100");
        System.out.println(bd1.divide(bd2,BigDecimal.ROUND_FLOOR,3));
    }
}

注意 在除法运算的时候要用divide()方法的参数类型来进行小数位数的控制