java数据运算常见ApI(Math、BigInteger、BigDecimal)的学习

Math类

  • abs 绝对值
  • round 四舍五入
  • ceil 向上取整
  • floor 向下取整
  • max 求最大值
  • min 求最小值
  • pow 次幂
package com.doit.demo;
/*
     java.lang.Math 数学工具类
       构造方法私有 所有方法静态

       静态常量
           public static final double PI;
       静态方法
           public static int  abs(int num);  绝对值
           pubic  static long round(double a) 四舍五入
           pubic  static int round(float a)
           public static double ceil(double num) 向上取整
           public static double floor(double num) 向下取整
           public static int max(int a,int b) 求最大值
           public static int min(int a,int b) 求最小值
           public static double pow(double a,double b)  a的b次幂

 */
public class Demo01_Math {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        //绝对值
        System.out.println(Math.abs(-10));
        //四舍五入
        System.out.println(Math.round(5.5)); //6    long
        System.out.println(Math.round(5.1f));//5    int
        //向上取整
        System.out.println(Math.ceil(5.1));//6.0
        System.out.println(Math.ceil(-5.1));//-5.0
        //向下取整
        System.out.println(Math.floor(5.9));//5.0
        System.out.println(Math.floor(-5.9));//-6.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,3));//8.0

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

    }
}

BigInteger类

  • public BigInteger(String val)
  • add(BigInteger value)
  • subtract(BigInteger value)
  • multiply(BigInteger value)
  • divide(BigInteger value)
  • intValues() longValue()将BigInteger转换成int或者long型数据
package com.doit.demo;

import java.math.BigInteger;

/*
    java.math.BigInteger
            进行大整数的运算
            构造方法
                public BigInteger(String val)
            方法         
                add(BigInteger value) 返回其值为 (this + val) 的 BigInteger,超大整数加法运算
                subtract(BigInteger value)返回其值为 (this - val) 的 BigInteger,超大整数减法运算
                multiply(BigInteger value)返回其值为 (this * val) 的 BigInteger,超大整数乘法运算
                divide(BigInteger value)返回其值为 (this / val) 的 BigInteger,超大整数除法运算,除不尽取整数部分
                
            BigInteger可以调用intValues()  和   longValue将BigInteger对象转换成int  或者  long

 */
public class Demo02_BigInteger {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("2321312142132132131421556575766544324434324");
        BigInteger b2 = new BigInteger("21556575766544324434324");


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

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


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


        //除法  整数整除
        BigInteger divide = b1.divide(b2);
        System.out.println(divide);


        BigInteger b3 = new BigInteger("100");
        BigInteger b4 = new BigInteger("200");

		//除不尽取整数部分
        BigInteger bi = b3.add(b4);
        //转换成int 或者 long
        int i = bi.intValue();
        System.out.println(i);
        long l = bi.longValue();
        System.out.println(l);


    }
}

BigDecimal类

  • 精确的小数的计算
  • add(BigDecimal augend)
  • subtract
  • multiply
  • divide(BigDecimal divisor,int scale,int roundingMode)
  • 除法要注意
package com.doit.demo;

import java.math.BigDecimal;

/*
    java.math.BigDecimal
            作用  精确的小数的计算
            构造方法
                public BigDecimal(String value)

            方法
                 BigDecimal add(BigDecimal augend) 正常加法
                 BigDecimal subtract(BigDecimal subtrahend) 减法
                 BigDecimal multiply(BigDecimal multiplicand)
                 BigDecimal divide(BigDecimal divisor)  除法


                    - 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 class Demo03_BigDecimal {
    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);//String
        System.out.println(add.doubleValue());//将计算得到的数转化成小数

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

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


        //除法     除数   保留2位    保留的方式
        BigDecimal divide = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);


    }
}

String与数值相互转换

  • Integer下方法: parseInt(String str) string 转 数值
  • Double下方法: parseDouble(“10.1”) string 转 数值
  • String.valueOf(10); 数值转string
package com.doit.demo02;

/*
     java.lang.Integer
        静态方法
          static int parseInt(String str) 将字符串转换为基本类型 String --- int

 */
public class Demo01_Integer {
    public static void main(String[] args) {
        //String---->int/double  string  转 数值
        int num = Integer.parseInt("10");
        System.out.println(num);
        double b = Double.parseDouble("10.1");
        System.out.println(b);

        //int ---- > String    数值转string 
        String s= 10+"";
        String s1 = String.valueOf(10);

    }
}

Integer和int自动拆箱和装箱

package com.doit.demo02;

import java.util.ArrayList;

/*
     int ---- >Integer
          public Integer(int num)
          public Integer(String val)

     Integer ---->int
           方法
             int intValue()

     JDK1.5  以后

     自动装箱
        int --- > Integer
     自动拆箱
        Integer ---->int
 */
public class Demo02_Integer {
    public static void main(String[] args) {
        // int ---- >Integer
        int i = 10;
        Integer in = new Integer(i);


        //Integer --->int
        int num = in.intValue();
        System.out.println(num);

        System.out.println("--------------------------");
        //自动装箱
        Integer in2 = 10;//new Integer(10);

        //自动拆箱
        System.out.println(in2+100);//int2.intValue+100

        System.out.println("--------------------------------");
        ArrayList<Integer> list = new ArrayList<>();

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


        Integer n =  list.get(0);

        //自动拆箱
        int n2 = list.get(1);

    }
}

Integer需要注意的问题

package com.doit.demo02;

/**
 * byte范围内
 **/
public class Demo03_Integer {
    public static void main(String[] args) {
        Integer  i1 = new Integer(500);
        Integer  i2 = new Integer(500);
        System.out.println(i1 == i2);//false
        System.out.println(i1.equals(i2));//true


        Integer i3 =500;//new Integer(500);
        Integer i4 =500;//new Integer(500);
        System.out.println(i3 == i4);//false
        System.out.println(i3.equals(i4));//true


        //byte范围内
        Integer i5 =127;//new Integer(127);
        Integer i6 =127;//i6 = i5;
        System.out.println(i5 == i6);//true
        System.out.println(i5.equals(i6));//true


        System.out.println("-----------");
        //Integer.compare(a,b)方法a>b 1  a<b -1 a==b 0
        int i = Integer.compare(30, 20);
        System.out.println(i);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值