JavaMath类

2022/9/2 1:25:40

本文主要是介绍JavaMath类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

方法 描述
abs() 返回参数的绝对值
ceil() 对number类型变量向上取整,返回值类型为double类型
floor() 对number类型变量向下取整,返回值类型为double类型
rint() 返回与参数最接近的整数,返回值类型为double类型
round() 四舍五入
min() 返回两个参数中的最小值
max() 返回两个参数中的最大值
exp() 返回自然数底数为e的参数次方(返回值为e的x次方)
log() 返回参数的自然数底数的对数值
pow() 返回第一个参数的x次方
sqrt() 求参数的算数平方根
PI 圆周率(Π=3.1415926)
E 自然数(e=2.7182818)
sin() 求指定double类型参数的正弦值
cos() 求指定double类型参数的余弦值
tan() 求指定double类型参数的正切值
asin() 求指定double类型参数的反正弦值
acos() 求指定double类型参数的反余弦值
atan() 求指定double类型参数的反正切值
toDegrees() 将弧度转换成角度
toRadians() 将角度转化为弧度
random() 返回随机数
public class MathMethod {
    public static void main(String[] args) {
        // abs() 返回参数的绝对值
        int a = -10;
        System.out.println(Math.abs(a));  // 10
        // ceil() 对number类型变量向上取整,返回值类型为double类型
        double b = 0.98;
        System.out.println(Math.ceil(b));  // 1.0
        // ceil() 对number类型变量向下取整,返回值类型为double类型
        double c = 0.45;
        System.out.println(Math.floor(c));  // 0.0
        // rint() 返回与参数最接近的整数,返回值类型为double类型
        System.out.println(Math.rint(b));  // 1.0
        System.out.println(Math.rint(c));  // 0.0
        // round() 四舍五入 返回值int
        double d = 1.5;
        System.out.println(Math.round(d));  // 2
        int e = 3;
        double f = 2.5;
        // min() 求两个参数之间的最小值 返回值double
        System.out.println(Math.min(e, f));  // 2.5
        // max() 求两个参数之间的最大值 返回值double
        System.out.println(Math.max(e, f));  // 3.0
        // exp() 求e的x次方 返回值double
        int g = 2;
        System.out.println(Math.exp(g));  // 7.38905609893065
        // log() 求以自然数为底的对数值 e=2.7182818 返回值double
        double h = 11.635;
        System.out.println(Math.log(h));  // 2.454017796754255
        // log() 求以10为底数的对数值 返回值double
        double i = 100;
        System.out.println(Math.log10(i));  // 2.0
        // pow(a, x) 求a的x次方 返回值double
        int j = 4;
        System.out.println(Math.pow(j, 2));  // 16.0
        // sqrt() 求参数的算数平方根 返回值double
        int k = 16;
        System.out.println(Math.sqrt(k));  // 4.0
        // PI 圆周率 返回值double
        System.out.println(Math.PI);  // 3.141592653589793
        // E 自然数
        System.out.println(Math.E);  // 2.718281828459045
        // sin() 求指定double类型参数的正弦值
        System.out.println(Math.sin(Math.PI/2));  // 1.0
        // cos() 求指定double类型参数的余弦值
        System.out.println(Math.cos(Math.PI));  // -1.0
        // tan() 求指定double类型参数的正切值
        System.out.println(Math.tan(Math.PI/4));  // 0.9999999999999999
        // asin() 求指定double类型参数的反正弦值(通过正弦值计算角度,输出的是弧度制)
        System.out.println(Math.asin(0.5));  // 0.5235987755982989(Π/6)
        // acos() 求指定double类型参数的反余弦值(通过余弦值计算角度,输出的是弧度制)
        System.out.println(Math.acos(0.5));  // 1.0471975511965979(Π/3)
        // atan() 求指定double类型参数的反正切值(通过正切值计算角度,输出的是弧度制)
        System.out.println(Math.atan(1));  // 0.7853981633974483(Π/4)
        // toDegrees() 将弧度转换成角度
        System.out.println(Math.toDegrees(Math.PI/4));  // 45.0
        // toRadians() 将角度转化为弧度
        int l = 45;
        System.out.println(Math.toRadians(l));  // 0.7853981633974483(Π/4)
        // random() 返回随机数(产生一个[0,1)之间的随机数。)
        System.out.println(Math.random());  // 0.975273125745941
        //返回指定范围的随机数(m-n之间)的公式:***
        int m = 10;
        int n = 20;
        System.out.println(Math.random()*(n-m)+m);  // 17.296386035409093
        System.out.println(Math.random()*(n+1-m)+m);  // 17.066982131401616

    }
}



这篇关于JavaMath类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程