Math与Big类的API,有点小多

Java.lang.Math        数学工具类

方法

        publuc static int        abs(int a) 返回参数的绝对值

        public static double        ceil(double a) 返回大于或者等于参数的最小double值,等于一个整数

        public static double floor(double a) 返回小于或者等于参数的最大double值,等于一个整数

        public static int round(float a) 按照四舍五入返回最接近参数的int

        public static double pow(double a,double b) 返回a的b次幂

        public static double random() 生成随机小数[0,1)

//绝对值
System.out.println(Math.abs(-10)); //10
//四舍五入
System.out.println(Math.round(3.3));//3
System.out.println(Math.round(3.5f));//4

//向上取整
System.out.println(Math.ceil(10.1));//11.0
System.out.println(Math.ceil(10.9));//11.0
System.out.println(Math.ceil(-10.9));//-10.0

//向下取整
System.out.println(Math.floor(10.1));//10.0
System.out.println(Math.floor(10.9));//10.0
System.out.println(Math.floor(-10.9));//11.0

//最大值 最小值
System.out.println(Math.max(10,20));//20
System.out.println(Math.min(10,20));//10

//a的b次幂
System.out.println(Math.pow(2,4));//16.0

//生成随机小数
System.out.println(Math.random());//[0,1)

java.math.BigInteger        精确的大整数计算

构造方法

        BigInteger(String val)

方法

        BigInteger  add(BigInteger value) 返回值为(this+value) 的BigInteger,超大整数加法运算

        BigInteger subtract(BigInteger value) 返回值为(this-value) 的BigInteger,超大整数减法运算

        BigInteger multiply(BigInteger value) 返回值为(this*value)的BigInteger,超大整数乘法运算

        BigInteger divide(BigInteger value) 返回值为(this/value)的BigInteger,超大整数除法运算

//加法
BigInteger add = b1.add(b2);
System.out.println(add.toString());

//减法
BigInteger subtract = b1.subtract(b2);
System.out.println(subtract);

//乘法
BigInteger multiply = b1.multiply(b2);
System.out.println(multiply);

//除法 只要整数 小数直接舍去
BigInteger divide = b1.divide(b2);
System.out.println(divide);

java.math.BigDecimal        精确的小数计算

构造方法

        public BigDecimal(String val)

加减法与乘法跟BigInteger的运算方法一致

        除法

         - BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
         - divesor:此 BigDecimal 要除以的值。
         - scale:保留的位数
         - roundingMode:舍入方式
         - 舍入方式:BigDecimal类提供静态的成员变量来表示舍入的方式
         - BigDecimal.ROUND_UP  向上加1。
         - BigDecimal.ROUND_DOWN 直接舍去。
         - BigDecimal.ROUND_HALF_UP 四舍五入。

public static void main(String[] args) {
    BigDecimal b1 = new BigDecimal("0.01");
    BigDecimal b2 = new BigDecimal("0.03");


    //加法
    BigDecimal add = b1.add(b2);
    System.out.println(add.toString());
    System.out.println(add.doubleValue());
    System.out.println(add.floatValue());


    //减法
    BigDecimal subtract = b1.subtract(b2);
    System.out.println(subtract);

    //乘法
    BigDecimal multiply = b1.multiply(b2);
    System.out.println(multiply);


    //除法
    BigDecimal divide = b1.divide(b2,2,BigDecimal.ROUND_UP);
    System.out.println(divide);

字符串和基本类型之间的转换

都是基本的包装类所有的静态方法

public static void main(String[] args) {

    //String --- > int
    int a = Integer.parseInt("10");
    System.out.println(a);

    //String ---->boolean
    boolean b = Boolean.parseBoolean("true");
    System.out.println(b);

    //String ---> double
    double c = Double.parseDouble("10.1");
    System.out.println(c);


    //基本类型 ----> String
    String s = 10+"";

    String s1 = String.valueOf(10);

}

基本类型与包装类型的转换

jdk1.5以后已经可以自动拆装箱.

Jdk的版本实在是低的话,可以new一个Integer的对象完成装箱功能

        new Integer(int num);

        new Integer(String num);

// int ---- > Integer 装箱

Integer in = new Integer(10);

//Integer ---- > int 拆箱
System.out.println(in.intValue());

ArrayList<Integer> list = new ArrayList<>();


list.add(new Integer(10));
list.add(10);//自动装箱

Integer in3 =  list.get(0);
int num = list.get(0); //自动拆箱

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值