java-Math类

一.Math类概念

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类常用方法

//abs方法,结果都为正数
double d1 = Math.abs(-5); // d1的值为5
double d2 = Math.abs(5); // d2的值为5

//ceil方法,结果为比参数值大的最小整数的double值
double d1 = Math.ceil(3.3); //d1的值为 4.0
double d2 = Math.ceil(-3.3); //d2的值为 -3.0
double d3 = Math.ceil(5.1); // d3的值为 6.0

//floor方法,结果为比参数值小的最大整数的double值
double d1 = Math.floor(3.3); //d1的值为3.0
double d2 = Math.floor(-3.3); //d2的值为-4.0
double d3 = Math.floor(5.1); //d3的值为 5.0

//max方法,返回两个参数值中较大的值
double d1 = Math.max(3.3, 5.5); //d1的值为5.5
double d2 = Math.max(-3.3, -5.5); //d2的值为-3.3

//min方法,返回两个参数值中较小的值
double d1 = Math.min(3.3, 5.5); //d1的值为3.3
double d2 = Math.max(-3.3, -5.5); //d2的值为-5.5

//pow方法,返回第一个参数的第二个参数次幂的值
double d1 = Math.pow(2.0, 3.0); //d1的值为 8.0
double d2 = Math.pow(3.0, 3.0); //d2的值为27.0

//round方法,返回参数值四舍五入的结果
double d1 = Math.round(5.5); //d1的值为6.0
double d2 = Math.round(5.4); //d2的值为5.0

//random方法,产生一个大于等于0.0且小于1.0的double小数
double d1 = Math.random();
原文地址:https://www.cnblogs.com/akiyama/p/10222908.html