Java Math类
Math类:
abs()
返回参数的绝对值。
ceil()
对整形变量向左取整,返回类型为double型。
floor()
对整型变量向右取整。返回类型为double类型。
rint()
返回与参数最接近的整数。返回类型为double。
round()
返回一个最接近的int、long型值。
min()
返回两个参数中的最小值。
max()
返回两个参数中的最大值。
exp()
返回自然数底数e的参数次方。
log()
返回参数的自然数底数的对数值。
pow()
返回第一个参数的第二个参数次方。
sqrt()
求参数的算术平方根。
sin()
求指定double类型参数的正弦值。
cos()
求指定double类型参数的余弦值。
tan()
求指定double类型参数的正切值。
asin()
求指定double类型参数的反正弦值。
acos()
求指定double类型参数的反余弦值。
atan()
求指定double类型参数的反正切值。
atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。
toDegrees()
将参数转化为角度。
toRadians()
将角度转换为弧度。
random()
返回一个随机数。
public class MathTest { public static void main(String args[]) { //e常数 System.out.println("Math.E:"+Math.E); //pi常数 System.out.println("Math.Pi:"+Math.PI); //取整 float f=100.544f; System.out.println("round:"+Math.round(f));//四舍五入取整,返回的是int System.out.println("ceil:"+Math.ceil(f));//向上取整 System.out.println("floor:"+Math.floor(f));//向下取整 System.out.println("rint:"+Math.rint(f));//最接近的整数取整 //sin\cos\tan double s=0.66f; System.out.println("sin:"+Math.sin(s)); System.out.println("cos:"+Math.cos(s)); System.out.println("tan:"+Math.tan(s)); //对数、开方、幂、exp System.out.println("幂pow:"+Math.pow(s,s)); System.out.println("开方sqrt:"+Math.sqrt(s)); System.out.println("对数log:"+Math.log(s));//自然对数。想要不同的底数可用换底公式 System.out.println("e次幂:"+Math.exp(s)); //绝对值。随机数 System.out.println("绝对值abs:"+Math.abs(s)); System.out.println("随机数:"+Math.random()); } }
结果
Math.E:2.718281828459045 Math.Pi:3.141592653589793 round:101 ceil:101.0 floor:100.0 rint:101.0 sin:0.6131168726918044 cos:0.7899922154177355 tan:0.7761049548666727 幂pow:0.7601494823907127 开方sqrt:0.81240385660461 对数log:-0.41551540422523675 e次幂:1.9347923851439806 绝对值abs:0.6600000262260437 随机数:0.3822831792072645

浙公网安备 33010602011771号