Java - Numbers Class

本文详细介绍了Java中数字类的各种方法,包括数值转换、比较、数学运算等,如xxxValue(), compareTo(), abs(), ceil(), floor(), rint(), round(), min(), max(), exp(), log(), pow(), sqrt(), sin(), cos(), tan()等,并提供了实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java - Numbers Class

Number Methods
NO.Method && Description
1xxxValue()
转换为指定类型的值
2compareTo()
比较值的大小
3equals()
判断大小
4valueOf()
返回一个整数对象并且持有指定的值
5toString()
返回一个字符串对象代表当前指定的int or integer
6parseInt()
方法用于格式化一些字符串返回整数
7abs()
求绝对值
8ceil()
向上取整 , 返回double
9floor()
向下取整, 返回 double
10rint()
四舍五入取整数,返回double
11round()
四舍五入
12min()
取最小值
13max()
取最大值
14exp()
对数e的计算
15log()
返回自然的对数
16pow()
阶运算,第一个参数做底数,第二参数做阶数
17sqrt()
获取平方根
18sin()
正弦
19cos()
余弦
20tan()
正切
21asin()
反正弦
22acos()
反余弦
23atan()
反正切
24atan2()
直角坐标转换为极坐标
25toDegrees()
转换为角度
26toRadians()
转换为弧度
27random()
随机数

Example

public class Methods {
    public static void main(String[] args) {
        Integer x = 8;
        // XXXValue()
        System.out.println("Number.xxxValue()");
        // return byte primitive data type
        System.out.println(x.byteValue());
        // return double primitive data type
        System.out.println(x.doubleValue());
        // return long primitive data type
        System.out.println(x.longValue());

        // compareTo();
        System.out.println("Number.compareTo()");
        // return 1
        System.out.println(x.compareTo(6));
        // return 0
        System.out.println(x.compareTo(8));
        // return -1
        System.out.println(x.compareTo(10));

        // equals()
        System.out.println("Number.equals()");
        Integer y = 8;
        Integer z = 9;
        short h = 8;
        String m = "8";
        System.out.println(x.equals(y));
        System.out.println(x.equals(z));
        System.out.println(x.equals(h));
        System.out.println(x.equals(m));


        // valueOf()
        System.out.println("Number.valueOf()");
        System.out.println(Double.valueOf("99"));
        System.out.println(Float.valueOf("90"));
        System.out.println(Long.valueOf("999"));
        System.out.println(Integer.valueOf("9", 16));

        // toString
        System.out.println("Number.toString()");
        String s1 = Integer.toString(8);
        System.out.println(s1 instanceof String);

        // parseInt()
        System.out.println("parseInt()");
        System.out.println(Integer.parseInt("666"));
        System.out.println(Double.parseDouble("888"));
        System.out.println(Float.parseFloat("777"));

        // abs()
        System.out.println("Math.abs()");
        System.out.println(Math.abs(Integer.parseInt("-99")));
        System.out.println(Math.abs(Double.parseDouble("-88.88")));

        // ceil() return double type
        double d1 = -100.81;
        float f1 = 99;
        System.out.println("Math.ceil()");
        System.out.println(Math.ceil(d1));
        System.out.println(Math.ceil(f1));

        // floor()
        System.out.println("Math.floor()");
        System.out.println(Math.floor(-99.99));
        System.out.println(Math.floor(88.10));

        // rint()
        System.out.println("Math.rint()");
        System.out.println(Math.rint(99.1));
        System.out.println(Math.rint(99.5));
        System.out.println(Math.rint(99.6));

        // round()
        System.out.println("Math.round()");
        System.out.println(Math.round(99.1));
        System.out.println(Math.round(99.9));

        // min()
        System.out.println("Math.min()");
        System.out.println(Math.min(11, 44.1));

        // max()
        System.out.println("Math.max()");
        System.out.println(Math.max(11, 44.1));

        // exp() 对数
        System.out.println("Math.exp()");
        System.out.println(Math.E);
        System.out.println(Math.exp(2));

        // log()
        System.out.println("Math.log()");
        System.out.println(Math.log(7.39));

        // pow
        System.out.println("Math.pow");
        System.out.println(Math.pow(2, 5));

        // sqrt
        System.out.println("Math.sqrt");
        System.out.println(Math.sqrt(2));

        // sin
        double degrees = 60.0;
        double radians = Math.toRadians(degrees);

        System.out.println("Math.sin()");
        System.out.println(radians);
        System.out.println(Math.sin(radians));
        System.out.println(Math.sqrt(3) / 2);

        // cos
        System.out.println("Math.cos()");
        System.out.println(Math.cos(radians));

        // tan
        System.out.println("Math.tan()");
        System.out.println(Math.tan(radians));

        // asin
        System.out.println("Math.asin()");
        System.out.println(Math.asin(Math.sin(radians)) == radians);

        // acos
        System.out.println("Math.acos()");
        System.out.println(Math.acos(Math.cos(radians)) == radians);

        // atan
        System.out.println("Math.atan()");
        System.out.println(Math.atan(Math.tan(radians)) == radians);

        // atan2
        System.out.println("Math.atan2()");
        System.out.println(Math.atan2(30, 45));

        //toDegrees
        System.out.println("Math.toDegrees()");
        System.out.println(Math.round(Math.toDegrees(radians)));

        //toRadians
        System.out.println("Math.toRadians()");
        System.out.println(Math.toRadians(degrees));

        //random
        System.out.println("Math.random()");
        System.out.println(Math.round(Math.random() * 1000 + 1));

    }
}

Output

Number.xxxValue()
8
8.0
8
Number.compareTo()
1
0
-1
Number.equals()
true
false
false
false
Number.valueOf()
99.0
90.0
999
9
Number.toString()
true
parseInt()
666
888.0
777.0
Math.abs()
99
88.88
Math.ceil()
-100.0
99.0
Math.floor()
-100.0
88.0
Math.rint()
99.0
100.0
100.0
Math.round()
99
100
Math.min()
11.0
Math.max()
44.1
Math.exp()
2.718281828459045
7.38905609893065
Math.log()
2.0001277349601105
Math.pow
32.0
Math.sqrt
1.4142135623730951
Math.sin()
1.0471975511965976
0.8660254037844386
0.8660254037844386
Math.cos()
0.5000000000000001
Math.tan()
1.7320508075688767
Math.asin()
true
Math.acos()
true
Math.atan()
true
Math.atan2()
0.5880026035475675
Math.toDegrees()
60
Math.toRadians()
1.0471975511965976
Math.random()
199
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值