Java中的Math类相关方法整理

最近在复习java相关知识,今儿整理了一下Math类常用的方法——

Math类主要提供了一些执行数学基本运算的方法,如三角函数、指数函数、取整函数、最大值、最小值、绝对值、随机数等等,由于实际工作中,三角函数、指数函数运用较少,本次不做分享,其余常用函数代码实例如下:

import java.math.BigInteger;
import java.util.Random;

public class MathPractice {

    public static int GetEvenNum(double num1, double num2) {
        //在num1和num2之间随机取一个偶数的方法
        int s = (int) num1 + (int) (Math.random() * (num2 - num1));
        /*
         * 这个不是很好理解,要先知道,利用Math.random()函数可以取得[0,1)之间的随机小数。
         * 那么 m+(Math.random()*n) 最小可以获取到的数是m,最大可以获取的数是无限小于m+n
         * 即 m+(Math.random()*n)的取值范围为:[m,m+n)
         * 那么要取两数值之间的随机整数,需要乘上一个系数再转成整形,即 m+(int)(Math.random()*(m-n))
         * */

        if (s % 2 == 0) {
            return s;
        } else {
            return s + 1;
        }
    }

    public static char GetRandomChar(char ch1, char ch2) {
        //使用Math类的random()方法也可以用来生成随机字符
        return (char) (ch1 + (char) (Math.random() * (ch2 - ch1 + 1)));
    }

    public static void main(String[] args) {
        System.out.println("================取整练习================");
        //向上取整
        System.out.println("使用ceil()方法取整:" + Math.ceil(3.5));
        //向下取整
        System.out.println("使用floor()方法取整:" + Math.floor(2.5));
        //返回与参数最接近的整数
        System.out.println("使用rint()方法取整:" + Math.rint(4.7));
        //返回与参数最接近的整数
        System.out.println("使用rint()方法取整:" + Math.rint(4.5));
        //将参数加上0.5后返回整数部分
        System.out.println("使用round()方法取整:" + Math.round(5.4f));
        //将参数加上0.5后返回整数部分,并将结果强制转换为长整型
        System.out.println("使用round()方法取整:" + Math.round(2.5));

        System.out.println("================取值练习================");
        //取两个参数最大值
        System.out.println("4和5较大的是:" + Math.max(4, 5));
        //取两个参数最小值
        System.out.println("6.6和6较大的是:" + Math.min(6.6, 6));
        //取绝对值
        System.out.println("-8的绝对值是:" + Math.abs(-8));

        System.out.println("================调用生成随机偶数的方法================");
        System.out.println("生成任意一个2~64之间偶数的结果为:" + GetEvenNum(2, 64));

        System.out.println("================调用生成随机字符的方法================");
        System.out.println("获取a~z之间的随机字符:" + GetRandomChar('a', 'z'));
        System.out.println("获取A~Z之间的随机字符:" + GetRandomChar('A', 'Z'));
        System.out.println("获取0~9之间的随机字符:" + GetRandomChar('0', '9'));

        System.out.println("================使用Random类创建一个随机数生成器================");
        Random r = new Random();
        System.out.println("随机取一个整数:" + r.nextInt());
        System.out.println("随机取一个大于等于0小于10的整数:" + r.nextInt(10));
        System.out.println("随机取一个boolean值:" + r.nextBoolean());
        System.out.println("随机取一个双精度数值:" + r.nextDouble());
        System.out.println("随机取一个浮点型数值:" + r.nextFloat());
        System.out.println("随机产生一个概率密度为高斯分布的双精度数值:" + r.nextGaussian());

        System.out.println("================使用Random类创建一个随机数生成器================");
        //BigInteger 支持任意精度的整数,注意:BigInteger()中的参数都是以字符串形式存在的
        BigInteger bi = new BigInteger("4");
        System.out.println("大数字加法:" + bi.add(new BigInteger("2")));
        System.out.println("大数字减法:" + bi.subtract(new BigInteger("1")));
        System.out.println("大数字乘法:" + bi.multiply(new BigInteger("4")));
        System.out.println("大数字除法:" + bi.divide(new BigInteger("3")));
        // 用divideAndRemainder()方法做除法,结果为数组,[0]为商,[1]为余数
        System.out.println("大数字取商:" + bi.divideAndRemainder(new BigInteger("3"))[0]);
        System.out.println("大数字取余:" + bi.divideAndRemainder(new BigInteger("3"))[1]);
        System.out.println("大数字次方:" + bi.pow(4));
        System.out.println("大数字取反:" + bi.negate());

    }

}

此外,针对大数字的运算,还有BigDecimal类,表示任意精度的定点数,可以用来精确计算货币值 。由于BigDecimal 涉及内容较多,后面会单独写一篇blog用作介绍。

==================================================================

它来了,它来了,它架着梅利号闯来了!

BigDecimal类相关的内容请参考下面的这篇:

Java中BigDecimal类的常见用法及针对divide()方法的多种处理模式_花生君的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/m0_54701273/article/details/128413676