Java - 工具类2(数学相关) July 11,2019

一、Math类:

java.lang.Object

     java.lang.Math

所属的包为java.lang包,所以不需要导包;

Math构造方法是私有的  我们不能直接调用创建对象;

由于Math中提供的属性及方法都是static  不需要创建对象。

 

1.Math.abs()

public class TestMath {
    public static void main(String[] args){
        System.out.println(Math.abs(-1));//返回绝对值
        System.out.println(Math.abs(-3.2));
        System.out.println(Math.abs(1));

    }
}
输出结果:
1
3.2
1

2.Math.ceil()与Math.floor()

public class TestMath {
    public static void main(String[] args){
        System.out.println(Math.ceil(1.4));//向上取整
        System.out.println(Math.ceil(1.6));//向上取整
        System.out.println(Math.ceil(2.1));//向上取整
        System.out.println(Math.floor(1.4));//向下取整
        System.out.println(Math.floor(1.6));//向下取整
        System.out.println(Math.floor(2.1));//向下取整
    }
}
输出结果:
2.0
2.0
3.0
1.0
1.0
2.0

3.Math.rint()

public class TestMath {
    public static void main(String[] args){
            System.out.println(Math.rint(1.4));//取临近的整数,如果两边距离一样 则返回偶数
            System.out.println(Math.rint(1.6));
            System.out.println(Math.rint(2.3));
            System.out.println(Math.rint(2.5));
    }
}
输出结果:
1.0
2.0
2.0
2.0

4.Math.round()

public class TestMath {
    public static void main(String[] args){
        System.out.println(Math.round(4.6));//四舍五入取整
        System.out.println(Math.round(4.5));
        System.out.println(Math.round(4.4));
    }
}
输出结果:
5
5
4

 5.max()、min()、pow()、sqrt()

public class TestMath {
    public static void main(String[] args){
        System.out.println(Math.max(1,2));//比较两数,输出最大值
        System.out.println(Math.max(1.2,2.1));
        System.out.println(Math.min(1,2));//比较输出最小值
        System.out.println(Math.min(1.2,2.1));
        System.out.println(Math.pow(2,3));//2^3
        System.out.println(Math.sqrt(9));//平方根
    }
}
输出结果:
2
2.1
1
1.2
8.0
3.0

 6.Math.random()

public class TestMath {
    public static void main(String[] args){
        System.out.println(Math.random());//产生[0.0-1.0)的随机数
        System.out.println(Math.random());
    }
}
输出结果:
0.836120488165103
0.11473892544461917

//一般随机数的话 使用Random类提供的方法

二、Random类

java.lang.Object

  java.util.Random

1.在java.util包中的类,需要import导入;
 2.没有任何继承关系,默认继承Object类;
 3.查找构造方法--->如何创建对象
    Random r = new Random();
 4.类中提供的常用方法
    r.nextInt();   随机产生 int取值范围的整数 有正有负
    r.nextInt(int bound);   随机产生一个  [0--bound)  整数 
       注意bound必须为正数  否则会出现如下的运行时异常
            IllegalArgumentException
    r.nextFloat()  随机产生一个 [0.0---1.0)
    r.nextBoolean()   随机产生一个boolean值   true  false


import java.util.Random;

public
class TestMath { public static void main(String[] args){ Random random = new Random(); int value1 = random.nextInt();//产生一个随机数 int范围之内的 int value2 = random.nextInt(10);//产生一个[0,bound)范围内的整数 float value3 = random.nextFloat();//产生一个[0,1)范围内的小数 boolean value4 = random.nextBoolean();//随机产生一个boolean值 } }

 三、UUID类

java.lang.Object    

  java.util.UUID

 1.所属的包为java.util,需要import导入;
 2.没有任何继承关系,默认继承Object类;
 3.有带参数的构造方法,没有无参构造方法,所以使用时我们通常不会创建对象,直接UUID uuid = UUID.randomUUID();

四、BigInteger类(处理大的整数)超过了long的范围   -2^63 ~  2^63-1

java.lang.Object

  java.lang.Number

    java.math.BigInteger

 

1.所属的包java.math  需要import导入
2.继承自Number  
3.如何创建对象   提供的构造方法全部都是带参数的
    通常利用带String参数的构造方法创建这个类的对象
    BigInteger  bi = new BigInteger("123"); 
4.类中的常用方法(一般此类被用来做四则运算) 
    add()   subtract()   multiply()   divide()

import java.math.BigInteger;
public
class TestMath { public static void main(String[] args){ BigInteger bigInteger = new BigInteger("123"); BigInteger bigInteger1 = new BigInteger("456"); System.out.println(bigInteger.add(bigInteger1)); System.out.println(bigInteger.subtract(bigInteger1)); System.out.println(bigInteger.multiply(bigInteger1)); System.out.println(bigInteger.divide(bigInteger1)); } } 输出结果: 579 -333 56088 0

5.设计一个方法 用来计算给定数字的阶乘:

 

import java.math.BigInteger;

public class TestMath {
    public BigInteger factorial(int value){
        BigInteger result = new BigInteger("1");
        for(int i=1;i<= value;i++){
            result = result.multiply(new BigInteger(i+""));//i+"" 是将i变为String类型, new 是让其变为BigInteger类型
        }
        return result;
    }

    public static void main(String[] args){
        TestMath testMath = new TestMath();
        BigInteger result = testMath.factorial(5);
        System.out.println(result);
    }
}
输出结果:
120

 五、BigDecima类    超过了double取值范围

java.lang.Object

  java.lang.Number

    java.math.BigDecimal


 1.所属的包  java.math包
 2.继承Number类
 3.通常也是可以通过 带String参数 构建对象
 4.类中的常用方法, 用来做四则运算
  add()   subtract()   multiply()   divide()
  两个参数前面是保留小数点之后的位数  后面参数是设置的模式
  对象.setScale(2,BigDecimal.ROUND_DOWN);

import java.math.BigDecimal;

public class TestMath {
    public static void main(String[] args){
        BigDecimal bigDecimal = new BigDecimal("123.459");
        bigDecimal = bigDecimal.setScale(2,BigDecimal.ROUND_DOWN);//小数点后保留的位数,向下取整
        System.out.println(bigDecimal);
    }
}
输出结果:
123.45

六、DecimalFormat

java.lang.Object

  java.text.Format

    java.text.NumberFormat

      java.text.DecimalFormat

 1.所属的包 java.text(text包是一个格式化包,包含各种格式化的方法)
 2.import导入才能使用
 3.通过带String参数的构造方法创建一个格式化对象
 4.调用format方法将一个小数格式化成一个字符串

import java.text.DecimalFormat;

public class TestMath {
    public static void main(String[] args){
        DecimalFormat decimalFormat = new DecimalFormat("000.###");//0表示必须有,#表示可有可无
        String result = decimalFormat.format(12.5);
        String result1 = decimalFormat.format(120.55);//0的位数不够时会补齐,#不够不会补齐
        String result2 = decimalFormat.format(1200.5555);//超过个数 直接四舍五入 
        System.out.println(result);
        System.out.println(result1);
        System.out.println(result2);

    }
}

输出结果:
012.5
120.55
1200.555

 

posted @ 2019-07-11 17:05  亿贫如洗杨道长  阅读(11)  评论(0编辑  收藏  举报