Java Number & Math 类

// java.lang.Math 常用

// xxxValue() 方法用于将 Number 对象转换为 xxx 数据类型的值并返回。
System.out.println(((Integer) 5).byteValue()); // 5 调用强制类型转换(byte)value

// compareTo() 方法用于将 Number 对象与方法的参数进行比较。可用于比较 Byte, Long, Integer等。
System.out.println(((Integer) 5).compareTo(5)); // 0 调用>,=关系运算

// equals() 方法用于判断 Number 对象与方法的参数进是否相等。
System.out.println(((Integer) 5).equals(((Integer) 5))); // true 调用instanceof判断数据类型,再调用==

// valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等。
System.out.println(Integer.valueOf(5)); // 5 调用对应的构造方法 new Integer(i)

// toString() 方法用于返回以一个字符串表示的 Number 对象值。
System.out.println(Integer.toString(12)); // 12 调用String类型的构造方法 new String(buf, true)

// parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析。
System.out.println(Integer.parseInt("5")); // 5

// abs() 返回参数的绝对值。参数可以是 int, float, long, double, short, byte类型。
System.out.println(Math.abs(5.4)); // 5.4 调用 (a <= 0.0D) ? 0.0D - a : a;

// ceil() 方法可对一个数进行上舍入,返回值大于或等于给定的参数。
System.out.println(Math.ceil(5.4)); // 6.0

// floor() 方法可对一个数进行下舍入,返回给定参数最大的整数,该整数小于或等给定的参数。
System.out.println(Math.floor(5.6)); // 5.0

// rint() 方法返回最接近参数的整数值。
System.out.println(Math.rint(5.5)); // 6.0

// round() 方法返回一个最接近的int、long型值。
System.out.println(Math.round(5.5)); // 6

// min() 方法用于返回两个参数中的最小值。
System.out.println(Math.min(5.5, 6)); // 5.5 调用 (a <= b) ? a : b

// max() 方法用于返回两个参数中的最大值。
System.out.println(Math.max(5.5, 6)); // 6.0 调用 (a >= b) ? a : b;

// exp() 方法用于返回自然数底数e的参数次方。
System.out.println(Math.exp(5)); // 148.4131591025766

// log() 方法用于返回参数的自然数底数的对数值。
System.out.println(Math.log(5)); // 1.6094379124341003

// pow() 方法用于返回第一个参数的第二个参数次方。
System.out.println(Math.pow(2, 2)); // 4.0

// sqrt() 方法用于返回参数的算术平方根。
System.out.println(Math.sqrt(4)); // 2.0

double degrees = 45.0;
// toRadians() 方法用于将角度转换为弧度。
double radians = Math.toRadians(degrees); // 0.7853981633974483 调用 angdeg / 180.0 * PI

// sin() 方法用于返回指定double类型参数的正弦值。
double sin = Math.sin(radians);
System.out.format("%.1f 度的正弦值为 %.4f%n", degrees, sin); // 45.0 度的正弦值为 0.7071

// cos() 方法用于返回指定double类型参数的余弦值。
double cos = Math.cos(radians);
System.out.format("%.1f 度的余弦值为 %.4f%n", degrees, cos); // 45.0 度的余弦值为 0.7071

// tan() 方法用于返回指定double类型参数的正切值。
double tan = Math.tan(radians);
System.out.format("%.1f 度的正切值是 %.4f%n", degrees, tan); // 45.0 度的正切值是 1.0000

// asin() 方法用于返回指定double类型参数的反正弦值。
double asin = Math.asin(sin);
// toDegrees() 方法用于将参数转化为角度。
System.out.format("%.4f 的反正弦值为 %.4f 度 %n", sin, Math.toDegrees(asin)); // 0.7071 的反正弦值为 45.0000 度 

// acos() 方法用于返回指定double类型参数的反余弦值。
double acos = Math.acos(cos);
System.out.format("%.4f 的反余弦值为 %.4f 度 %n", cos, Math.toDegrees(acos)); // 0.7071 的反余弦值为 45.0000 度 

// atan() 方法用于返回指定double类型参数的反正切值。
double atan = Math.atan(tan);
System.out.format("%.4f 的反正切值 %.4f 度 %n", tan, Math.toDegrees(atan)); // 1.0000 的反正切值 45.0000 度 

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

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

// 参考: http://www.runoob.com/java/java-number.html
// 中文api: http://www.runoob.com/manual/jdk1.6/java/lang/Math.html
// 官方api: https://docs.oracle.com/javase/8/docs/api/

猜你喜欢

转载自www.cnblogs.com/ooo0/p/9279246.html