Math类
数学类的方法都是静态方法,可以直接引用——Math.方法();
常用数学类方法:
- E 常量:e
- PI 常量:π
- max():求最大值
- min():求最小值
- abs():获取绝对值
- pow():求次幂
- round():四舍五入
- sqrt():求平方根
- cbrt():求立方根
- ceil():返回大于或等于 a 的最小整数
- floor():返回小于或等于 a 的最大整数
代码:
package MathKind;
//导入Math包
import java.lang.Math;
//import static java.lang.Math.max;//静态调用
public class MathK {
public static void main(String args[]){
//求e(自然对数)和 π(圆周率)
System.out.println(Math.E); //e=2.718281828459045
System.out.println(Math.PI); //π=3.141592653589793
//求最大值、最小值、绝对值
System.out.println(Math.max(8,22)); //22
System.out.println(Math.min(8,22)); //8
System.out.println(Math.abs(-18)); //18
//求次幂
System.out.println(Math.pow(5,3)); //125.0
//四舍五入
System.out.println(Math.round(8.7)); //9
System.out.println(Math.round(8.3)); //8
//求平方根
System.out.println(Math.sqrt(9)); //3.0
//求立方根
System.out.println(Math.cbrt(8)); //2.0
//返回大于或等于 a 的最小整数
System.out.println(Math.ceil(8.6)); //9.0
//返回小于或等于 a 的最大整数
System.out.println(Math.floor(7.8)); //7.0
}
}