likes
comments
collection
share

Java的Number & Math 类

作者站长头像
站长
· 阅读数 13

一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte、int、long、double 等。然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。为了解决这个问题,Java 语言为每一个内置数据类型提供了对应的包装类。所有的数值包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类(Number 类属于 java.lang 包)

包装类基本数据类型
Booleanboolean
Bytebyte
Shortshort
Integerint
Longlong
Characterchar
Floatfloat
Doubledouble

Java的Number & Math 类

这种由编译器特别支持的包装称为装箱,所以当内置数据类型被当作对象使用的时候,编译器会把内置类型装箱为包装类。相似的,编译器也可以把一个对象拆箱为内置类型。下面是一个使用 Integer 对象的实例,当 x 被赋为整型值时,由于x是一个对象,所以编译器要对x进行装箱。然后,为了使x能进行加运算,所以要对x进行拆箱。:

public class Test{
 
   public static void main(String[] args){
      Integer x = 5;
      x =  x + 10;
      System.out.println(x); 
   }
}
// 以上实例编译运行结果如下:
// 15

Java Math 类

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。例如:

public class Test {  
    public static void main (String []args)  
    {  
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));  
        System.out.println("0度的余弦值:" + Math.cos(0));  
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  
        System.out.println("1的反正切值: " + Math.atan(1));  
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  
        System.out.println("圆周率:" +Math.PI);
    }  
}
// 以上实例编译运行结果如下:
// 90 度的正弦值:1.0
// 0度的余弦值:1.0
// 60度的正切值:1.7320508075688767
// 1的反正切值: 0.7853981633974483
// π/2的角度值:90.0
// 圆周率:3.141592653589793

Number & Math 类方法

下面的表中列出的是 Number & Math 类常用的一些方法:

Number :

序号方法与描述
1xxxValue()方法,将 Number 对象转换为xxx数据类型的值并返回
2compareTo()方法,将number对象与参数比较
3equals()方法,判断number对象是否与参数相等
4valueOf()方法,返回一个 Number 对象指定的内置数据类型
5toString()方法,以字符串形式返回值
6parseInt()方法,将字符串解析为int类型

Math:

序号方法与描述
1abs()方法,返回参数的绝对值
2ceil()方法,向上取整,返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型
3floor()方法,向下取整,返回小于等于(<=)给定参数的最大整数,类型为双精度浮点型
4rint()方法,返回与参数最接近的整数。返回类型为double
5round()方法,它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11
6min()方法,返回两个参数中的最小值
7max()方法,返回两个参数中的最大值
8exp()方法,返回自然数底数e的参数次方
9log()方法,返回参数的自然数底数的对数值
10pow()方法,返回第一个参数的第二个参数次方
11sqrt()方法,求参数的算术平方根
12sin()方法,求指定double类型参数的正弦值
13cos()方法,求指定double类型参数的余弦值
14tan()方法,求指定double类型参数的正切值
15asin()方法,求指定double类型参数的反正弦值
16acos()方法,求指定double类型参数的反余弦值
17atan()方法,求指定double类型参数的反正切值
18atan2()方法,将笛卡尔坐标转换为极坐标,并返回极坐标的角度值
19toDegrees()方法,将参数转化为角度
20toRadians()方法,将角度转换为弧度
21random()方法,返回一个随机数,随机数范围为 0.0 =< Math.random < 1.0

方法实例:\color{red}{方法实例:}方法实例:

xxxValue() 方法实例

// 该方法有以下几种语法格式:
public byte byteValue()
public short shortValue()
public int intValue()
public long longValue()
public float floatValue()
public double doubleValue()

描述

xxxValue() 方法用于将 Number 对象转换为 xxx 数据类型的值并返回。

参数

  • 以上各函数不接受任何的参数。

返回值

转换为 xxx 类型后该对象表示的数值。

public class Test {
    public static void main(String[] args) {
        Integer x = 5;
        // 返回 byte 原生数据类型
        System.out.println( x.byteValue() );
        // 返回 short 原生数据类型
        System.out.println( x.shortValue() );
        // 返回 int 原生数据类型
        System.out.println( x.intValue() );
        // 返回 long 原生数据类型
        System.out.println( x.longValue() );
        // 返回 float 原生数据类型
        System.out.println(x.floatValue());
        // 返回 double 原生数据类型
        System.out.println(x.doubleValue());
    }
}

// 程序运行结果如下:
// 5
// 5
// 5
// 5
// 5.0
// 5.0

compareTo() 方法实例

public int compareTo( NumberSubClass referenceName )

描述

compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较

参数

  • referenceName -- 可以是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数。

返回值

如果指定的数与参数相等返回 0;如果指定的数小于参数返回 -1;如果指定的数大于参数返回 1。

public class Test {
    public static void main(String[] args) {
        Integer x = 5;
        System.out.println(x.compareTo(3));
        System.out.println(x.compareTo(5));
        System.out.println(x.compareTo(8));
    }
}

// 程序运行结果如下:
// 1
// 0
// -1

equals() 方法实例

public boolean equals(Object o)

描述

equals() 方法用于判断 Number 对象与方法的参数进是否相等。

参数

  • o -- 任何对象。

返回值

如 Number 对象不为 Null,且与方法的参数类型与数值都相等返回 True,否则返回 False。

public class Test {
    public static void main(String[] args) {
        Integer x = 5;
        Integer y = 10;
        Integer z =5;
        Short a = 5;

        System.out.println(x.equals(y));
        System.out.println(x.equals(z));
        System.out.println(x.equals(a));
    }
}

// 程序运行结果如下:
// false
// true
// false

valueOf() 方法实例

public static Integer valueOf(int i)
public static Integer valueOf(String s)
public static Integer valueOf(String s, int radix)

描述

valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等。该方法是静态方法。该方法可以接收两个参数一个是字符串,一个是基数。

参数

  • i -- Integer 对象的整数。
  • s -- Integer 对象的字符串。
  • radix --在解析字符串 s 时使用的进制数,用于指定使用的进制数。

返回值

  • Integer valueOf(int i):返回一个表示指定的 int 值的 Integer 实例。
  • Integer valueOf(String s):返回保存指定的 String 的值的 Integer 对象。
  • Integer valueOf(String s, int radix):返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值。
public class Test {
    public static void main(String[] args) {
        Integer x =Integer.valueOf(9);
        Double c = Double.valueOf(5);
        Float a = Float.valueOf("80");

        Integer b = Integer.valueOf("444",16);   // 使用 16 进制

        System.out.println(x);
        System.out.println(c);
        System.out.println(a);
        System.out.println(b);
    }
}

// 程序运行结果如下:
// 9
// 5.0
// 80.0
// 1092

toString() 方法实例

// 以 String 类为例,该方法有以下几种语法格式:
public String toString()
public static String toString(int i)

描述

toString() 方法用于返回以一个字符串表示的 Number 对象值。如果方法使用了原生的数据类型作为参数,返回原生数据类型的 String 对象值。如果方法有两个参数, 返回用第二个参数指定基数表示的第一个参数的字符串表示形式。

参数

  • i -- 要转换的整数。

返回值

  • toString(): 返回表示 Integer 值的 String 对象。
  • toString(int i): 返回表示指定 int 的 String 对象。
public class Test {
    public static void main(String[] args) {
        Integer x = 5;

        System.out.println(x.toString());
        System.out.println(Integer.toString(12));
    }
}

// 程序运行结果如下:
// 5
// 12

parseInt() 方法实例

// 所有 Number 派生类 parseInt 方法格式类似如下:
public static int parseInt(String s)
public static int parseInt(String s, int radix)

描述

parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析。如果方法有两个参数, 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

参数

  • s -- 十进制表示的字符串。
  • radix -- 指定的基数。

返回值

  • parseInt(String s):返回用十进制参数表示的整数值。
  • parseInt(int i):使用指定基数的字符串参数表示的整数 (基数可以是 10, 2, 8, 或 16 等进制数) 。
public class Test {
    public static void main(String[] args) {
        int x =Integer.parseInt("9");
        double c = Double.parseDouble("5");
        int b = Integer.parseInt("444",16);

        System.out.println(x);
        System.out.println(c);
        System.out.println(b);
    }
}

// 程序运行结果如下:
// 9
// 5.0
// 1092

abs() 方法实例

// 各个类型的方法格式类似如下:
public static double abs(double d)
public static float abs(float f)
public static int abs(int i)
public static long abs(long lng)

描述

abs() 返回参数的绝对值。参数可以是 int, float, long, double, short, byte类型。

参数

  • 任何原生数据类型。

返回值

返回参数的绝对值。

public class Test {
    public static void main(String[] args) {
        Integer a = -8;
        double d = -100;
        float f = -90f;

        System.out.println(Math.abs(a));
        System.out.println(Math.abs(d));
        System.out.println(Math.abs(f));
    }
}

// 程序运行结果如下:
// 8
// 100.0
// 90.0

rint() 方法实例

public static double rint(double d)

描述

rint() 方法返回最接近参数的整数值。

参数

  • d -- double 类型数据。

返回值

返回 double 类型数据,是最接近参数的整数值。

public class Test {
    public static void main(String[] args) {
        double d = 100.675;
        double e = 100.500;
        double f = 100.200;

        System.out.println(Math.rint(d));
        System.out.println(Math.rint(e));
        System.out.println(Math.rint(f));
    }
}

// 程序运行结果如下:
// 101.0
// 100.0
// 100.0

min() 方法实例

// 该方法有以下几种语法格式:
public static double min(double arg1, double arg2)
public static float min(float arg1, float arg2)
public static int min(int arg1, int arg2)
public static long min(long arg1, long arg2)

参数

  • 该方法接受两个原生数据类型作为参数。

返回值

返回两个参数中的最小值。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.min(12.123, 12.456));
        System.out.println(Math.min(23.12, 23.0));
    }
}

// 程序运行结果如下:
// 12.123
// 23.0

max() 方法实例

// 该方法有以下几种语法格式:
public static double max(double arg1, double arg2)
public static float max(float arg1, float arg2)
public static int max(int arg1, int arg2)
public static long max(long arg1, long arg2)

描述

max() 方法用于返回两个参数中的最大值。

参数

  • 该方法接受两个原生数据类型作为参数。

返回值

返回两个参数中的最大值。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.max(12.123, 18.456));
        System.out.println(Math.max(23.12, 23.0));
    }
}

// 程序运行结果如下:
// 18.456
// 23.12

exp() 方法实例

public static double exp(double d)

描述

exp() 方法用于返回自然数底数e的参数次方。

参数

  • d -- 任何原生数据类型。

返回值

返回自然数底数e的参数次方。

public class Test {
    public static void main(String[] args) {
        double x = 11.635;
        System.out.printf("自然底数 e 的值为 %.4f%n", Math.E);
        System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(x));
    }
}

// 程序运行结果如下:
// 自然底数 e 的值为 2.7183
// exp(11.635) 为 112983.831

log() 方法实例

public static double log(double d)

描述

log() 方法用于返回参数的自然数底数(以 e 为底)的对数值。

参数

  • d -- 任何原生数据类型。

返回值

返回参数的自然数底数的对数值。

public class Test {
    public static void main(String[] args) {
        double x = 11.635;
        System.out.printf("自然数底数 e 的值为 %.4f%n", Math.E);
        System.out.printf("log(%.3f) 为 %.3f%n", x, Math.log(x));
    }
}

// 程序运行结果如下:
// 自然数底数 e 的值为 2.7183
// log(11.635) 为 2.454

pow() 方法实例

public static double pow(double base, double exponent)

描述

pow() 方法用于返回第一个参数的第二个参数次方。

参数

  • base -- 任何原生数据类型。
  • exponent -- 任何原生数据类型。

返回值

返回第一个参数的第二个参数次方。

public class Test {
    public static void main(String[] args) {
        double x = 3.0;
        double y = 2.0;
        System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
    }
}

// 程序运行结果如下:
// pow(3.000, 2.000) 为 9.000

sqrt() 方法实例

public static double sqrt(double d)

描述

sqrt() 方法用于返回参数的算术平方根。

参数

  • d -- 任何原生数据类型。

返回值

返回参数的算术平方根。

public class Test {
    public static void main(String[] args) {
        double x = 16.0;
        System.out.printf("sqrt(%.3f) 为 %.3f%n", x, Math.sqrt(x));
    }
}

// 程序运行结果如下:
// sqrt(16.000) 为 4.000

sin() 方法实例

public static double sin(double d)

描述

sin() 方法用于返回指定double类型参数的正弦值。

参数

  • d -- 任何原生数据类型。

返回值

返回指定double类型参数的正弦值。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.1f 度的正弦值为 %.4f%n", degrees, Math.sin(radians));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 45.0 度的正弦值为 0.7071

cos() 方法实例

public static double cos(double d)

描述

cos() 方法用于返回指定double类型参数的余弦值。

参数

  • d -- 任何原生数据类型。

返回值

返回指定double类型参数的余弦值。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.1f 度的余弦值为 %.4f%n", degrees, Math.cos(radians));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 45.0 度的余弦值为 0.7071

tan() 方法实例

public static double tan(double d)

描述

tan() 方法用于返回指定double类型参数的正切值。

参数

  • d -- 任何原生数据类型。

返回值

返回指定double类型参数的正切值。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.1f 度的正切值是 %.4f%n", degrees, Math.tan(radians));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 45.0 度的正切值是 1.0000

asin() 方法实例

public static double asin(double d)

描述

asin() 方法用于返回指定double类型参数的反正弦值。

参数

  • d -- 任何原生数据类型。

返回值

返回指定double类型参数的反正弦值。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.4f 的反正弦值为 %.4f 度 %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 0.7071 的反正弦值为 45.0000 度 

acos() 方法实例

public static double acos(double d)

描述

acos() 方法用于返回指定 double 类型参数的反余弦值。

参数

  • d -- 任何原生数据类型,需要小于 1。

返回值

返回指定 double 类型参数的反余弦值,返回值在 0 到 Math.PI 之间。如果指定的参数大于 1,则返回 NaN。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.4f 的反余弦值为 %.4f 度 %n",
                Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 0.7071 的反余弦值为 45.0000 度 

atan() 方法实例

public static double atan(double d)

描述

atan() 方法用于返回指定double类型参数的反正切值。

参数

  • d -- 任何原生数据类型。

返回值

返回指定double类型参数的反正切值。

public class Test {
    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("pi 的值为 %.4f%n", Math.PI);
        System.out.format("%.4f 的反正切值 %.4f 度 %n", Math.cos(radians),
                Math.toDegrees(Math.atan(Math.sin(radians))));
    }
}

// 程序运行结果如下:
// pi 的值为 3.1416
// 0.7071 的反正切值 35.2644 度 

atan2() 方法实例

public static double atan2(double y, double x)

描述

atan2() 方法用于将矩形坐标 (x, y) 转换成极坐标 (r, theta),返回所得角 theta。该方法通过计算 y/x 的反正切值来计算相角 theta,范围为从 -pi 到 pi。

参数

  • y -- 纵坐标。
  • x -- 横坐标。

返回值

与笛卡儿坐标中点 (x, y) 对应的极坐标中点 (r, theta) 的 theta 组件。

public class Test {
    public static void main(String[] args) {
        double x = 45.0;
        double y = 30.0;

        System.out.println( Math.atan2(x, y) );
    }
}

// 程序运行结果如下:
// 0.982793723247329

toDegrees() 方法实例

public static double toDegrees(double d)

描述

toDegrees() 方法用于将参数转化为角度。

参数

  • d -- 任何原生数据类型。

返回值

该方法返回 double 值。

public class Test {
    public static void main(String[] args) {
        double x = Math.PI;
        double y = Math.PI / 2;

        System.out.println( Math.toDegrees(x) );
        System.out.println( Math.toDegrees(y) );
    }
}

// 程序运行结果如下:
// 180.0
// 90.0

toRadians() 方法实例

public static double toRadians(double d)

描述

toRadians() 方法用于将角度转换为弧度。

参数

  • d -- 任何原生数据类型。

返回值

该方法返回 double 值。

public class Test {
    public static void main(String[] args) {
        double x = 180.0;
        double y = 90.0;

        System.out.println( Math.toRadians(x) );
        System.out.println( Math.toRadians(y) );
    }
}

// 程序运行结果如下:
// 3.141592653589793
// 1.5707963267948966

random() 方法实例

public static double random()

描述

random() 方法用于返回一个随机数,随机数范围为 0.0 =< Math.random < 1.0。

参数

  • 这是一个默认方法,不接受任何参数。

返回值

该方法返回 double 值。

public class Test {
    public static void main(String[] args) {
        System.out.println( Math.random() );
        System.out.println( Math.random() );
    }
}

// 程序运行结果如下:
// 0.952365817422499
// 0.07852887259123176

ceil() 方法实例

// 该方法有以下几种语法格式:
public static double ceil(double d)
public static double ceil(float f)

描述

ceil() 方法可对一个数进行上舍入(向上取整),返回值大于或等于给定的参数,类型为双精度浮点型。

参数

  • double 或 float 的原生数据类型。

返回值

返回 double 类型,返回值大于或等于给定的参数。

public class Test {
    public static void main(String[] args) {
        double d = 100.475;
        float f = -90.6f;

        System.out.println(Math.ceil(d));
        System.out.println(Math.ceil(f));
    }
}

// 程序运行结果如下:
// 101.0
// -90.0

floor() 方法实例

// 该方法有以下几种语法格式:
public static double floor(double d)
public static double floor(float f)

描述

floor() 方法可对一个数进行下舍入(向下取整),返回给定参数最大的整数,该整数小于或等给定的参数。

参数

  • double 或 float 的原生数据类型。

返回值

返回 double 类型数据,小于或等于给定的参数。

public class Test {
    public static void main(String[] args) {
        double d = 100.675;
        float f = -90.6f;

        System.out.println(Math.floor(d));
        System.out.println(Math.floor(f));
    }
}

// 程序运行结果如下:
// 100.0
// -91.0

round() 方法实例

// 该方法有以下几种语法格式:
public static long round(double d)
public static int round(float f)

描述

表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整。

参数

  • d -- double 或 float 的原生数据类型
  • f -- float 原生数据类型

返回值

返回一个最接近的int、long型值,方法会指定返回的数据类型。

public class Test {
    public static void main(String[] args) {
        double d = 100.400;
        double e = 100.500;
        float f = - 100.4f;
        float g = - 90.5f;

        System.out.println(Math.round(d));
        System.out.println(Math.round(e));
        System.out.println(Math.round(f));
        System.out.println(Math.round(g));
    }
}

// 程序运行结果如下:
// 100
// 101
// -100
// -90

Math 的 floor,round 和 ceil 方法实例比较

参数Math.floorMath.roundMath.ceil
1.4112
1.5122
1.6122
-1.4-2-1-1
-1.5-2-1-1
-1.6-2-2-1