Math类概述及其成员方法

Math 类包括用于运行基本数学运算的方法,如初等指数、对数、平方根和三角函数,这个类寻常开发中用的不多,可是在某些需求上会用到,比方求二个用户年龄的相差多少岁,这会用到Math类中的方法!如今把Maht几个经常使用的方法列举一下,

public static int abs(int a) 求一个数的绝对值
public static double ceil(double a) 向上取大于这个数的最小整数
public static double floor(double a) 向下取这个值最大整数
public static int max(int a,int b) 求二个数据之间的最大的数
public static double pow(double a,double b) 某个值的平方根
public static double random() 返回0.0到1.0之间的值 包括0.0但不包括1.0
public static int round(float a)  四舍五入 round方法參数有二个一个时double类型。一个时float类型,假设传入的是double类型就返回float,假设是传入的是float类型返回的是int类型
public static double sqrt(double a)正平方根


如今请看样例代码

package cn.kge.com;

public class MathDemo {
	public static void main(String[] args) {
		System.out.println(Math.abs(-10.9));
		System.out.println(Math.ceil(12.1));
		System.out.println(Math.floor(12.9));
		System.out.println(Math.max(3, 8));
		System.out.println(Math.min(1, 9));
		System.out.println(Math.sqrt(16));
		System.out.println(Math.round(12.5f));
		System.out.println(Math.pow(2,5));
		System.out.println(Math.random());
	}
}

结果:

10.9
13.0
12.0
8
1
4.0
13
32.0
0.5078972850065835

原文地址:https://www.cnblogs.com/slgkaifa/p/7011108.html