基本概念
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);
}
}
常用方法
向下取整 Math.floor
四舍五入 Math.round
向上取整 Math.ceil
转换为xxx数据类型的值并返回 xxxValue()
将number对象与参数比较 compareTo()
判断number对象是否与参数相等 equals()
返回一个 Number 对象指定的内置数据类型 valueOf()
以字符串形式返回值 toString()
将字符串解析为int类型 parseInt()
返回参数的绝对值 abs()
返回与参数最接近的整数。返回类型为double rint()
返回两个参数中的最小值 min()
返回两个参数中的最大值 max()
返回自然数底数e的参数次方 exp()
返回参数的自然数底数的对数值 log()
返回第一个参数的第二个参数次方 pow()
求参数的算术平方根 sqrt()
求指定double类型参数的正弦值 sin()
求指定double类型参数的余弦值 cos()
求指定double类型参数的正切值 tan()
求指定double类型参数的反正弦值 asin()
求指定double类型参数的反余弦值 acos()
求指定double类型参数的反正切值 atan()
返回一个随机数 random()