Skip to main content

Number 和 Math 类

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);
}
}
res

90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793

方法

xxxValue

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

相关的方法有:

类型方法及描述
bytebyteValue() :以 byte 形式返回指定的数值。
abstract doubledoubleValue() :以 double 形式返回指定的数值。
abstract floatfloatValue() :以 float 形式返回指定的数值。
abstract intintValue() :以 int 形式返回指定的数值。
abstract longlongValue() :以 long 形式返回指定的数值。
shortshortValue() :以 short 形式返回指定的数值。

实例:

public class Test{ 

public static void main(String args[]){
Integer x = 5;
// 返回 byte 原生数据类型
System.out.println( x.byteValue() );

// 返回 double 原生数据类型
System.out.println(x.doubleValue());

// 返回 long 原生数据类型
System.out.println( x.longValue() );
}
}
res

5
5.0
5

compareTo

compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。

该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较。

public int compareTo( NumberSubClass referenceName )

参数

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));
}
}
res

1
0
-1

equals

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

参数

o -- 任何对象。

返回值

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

Double 和 Float 对象还有一些额外的条件,可以参阅 API 手册:JDK 1.6。

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));
}
}
res

false
true
false

toString

public class Test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
//输出的结果为 5 /n 12 /n
}
}

valueOf

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

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

参数

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);
}
}
res

9
5.0
80.0
1092

parseInt

parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析。

如果方法有两个参数, 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

static int parseInt(String s)
static int parseInt(String s, int radix)

参数

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);
}
}
res

9
5.0
1092

abs

取绝对值。

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));
}
}
res

8
100.0
90.0

ceilfloor

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

double ceil(double d)
double ceil(float f)
double floor(double d)
double floor(float f)

参数
double 或 float 的原生数据类型。
返回值
返回 double 类型,返回值大于或等于(小于或等于)给定的参数。

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

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

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

101.0
-90.0
100.0
-90.0

rint

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

语法
该方法有以下几种语法格式:

double rint(double 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));
}
}
res

101.0
100.0
100.0

round

round() 方法返回一个最接近的 int、long 型值,四舍五入。

round 表示"四舍五入",算法为 Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。

语法
该方法有以下几种语法格式:

long round(double d)
int round(float f)

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

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

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

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

101
101
100
90

min

返回最小值。

double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
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));
}
}
res

12.123
23.0

max

返回最大值。

double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)

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));
}
}
res

18.456
23.12

exp

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

double exp(double d)

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

System.out.printf("e 的值为 %.4f%n", Math.E);
System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(x));
}
}
res

e 的值为 2.7183
exp(11.635) 为 112983.831

log

取对数值。

double log(double d)

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

System.out.printf("e 的值为 %.4f%n", Math.E);
System.out.printf("log(%.3f) 为 %.3f%n", x, Math.log(x));
}
}
res

e 的值为 2.7183
log(11.635) 为 2.454

pow

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

double pow(double base, double exponent)

参数

  • base -- 任何原生数据类型。
  • exponent -- 任何原生数据类型。
public class Test{
public static void main(String args[]){
double x = 11.635;
double y = 2.76;

System.out.printf("e 的值为 %.4f%n", Math.E);
System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
}
}
res

e 的值为 2.7183
pow(11.635, 2.760) 为 874.008

sqrt

开方。

double sqrt(double d)

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

System.out.printf("e 的值为 %.4f%n", Math.E);
System.out.printf("sqrt(%.3f) 为 %.3f%n", x, Math.sqrt(x));
}
}
res

e 的值为 2.7183
sqrt(11.635) 为 3.411

sin

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));

}
}
res

pi 的值为 3.1416
45.0 度的正弦值为 0.7071

cos

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));

}
}
res

pi 的值为 3.1416
45.0 度的余弦值为 0.7071

tan

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));

}
}
res

pi 的值为 3.1416
45.0 度的正切值是 1.0000

asin

asin() 方法用于返回指定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))));

}
}
res

pi 的值为 3.1416
0.7071 的反正弦值为 45.0000 度

acos

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

public class RunoobTest {
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))));

}
}
res

pi 的值为 3.1416
0.7071 的反余弦值为 45.0000 度

atan

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))));

}
}
res

pi 的值为 3.1416
0.7071 的反正切值 35.2644 度

toDegrees

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

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

2578.3100780887044
1718.8733853924698

toRadians

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

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

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

0.7853981633974483
0.5235987755982988

random

返回一个随机数。

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

0.5444085967267008
0.7960235983184115