Java中math常用函数

  • Post author:
  • Post category:java




善于总结的小伙伴总是快乐的

public class MathDemo{
	public static void main(String[] args){
		//PI E属于Math的属性
		System.out.println("PI: "+Math.PI);
		System.out.println("E: "+Math.E);
		System.out.println("<--------------------------------------->");
		//abs求绝对值的函数    可以有多种重载,整型浮点型都可以,这里随便举两个例子。
		System.out.println("Math.abs(3.2): "+Math.abs(3.2));
		System.out.println("Math.abs(-3.2): "+Math.abs(-3.2));
		System.out.println("<--------------------------------------->");
		//ceil可以认为是求在坐标轴上,这个浮点数向右最近的整数。
		System.out.println("Math.ceil(3.2): "+Math.ceil(3.2));
		System.out.println("Math.ceil(-2.1): "+Math.ceil(-2.1));
		System.out.println("Math.ceil(3.9): "+Math.ceil(3.9));
		System.out.println("<--------------------------------------->");
		//floor舍弃小数部分,可以认为是求坐标轴上向左取整的数
		System.out.println("Math.floor(3.9): "+Math.floor(3.9));
		System.out.println("Math.floor(3.2): "+Math.floor(3.2));
		System.out.println("Math.floor(-2.1): "+Math.floor(-2.1));
		System.out.println("<--------------------------------------->");
		//round正常的四舍五入
		System.out.println("Math.round(3.2): "+Math.round(3.2));
		System.out.println("Math.round(3.5): "+Math.round(3.5));
		System.out.println("<--------------------------------------->");
		//max min求两个数的最大最小,有多种重载
		System.out.println("Math.max(1,6): "+Math.max(1,6));
		System.out.println("Math.min(1,6): "+Math.min(1,6));
		System.out.println("<--------------------------------------->");
		//random求一个随机数,不过范围只能在0.0~1.0之间,想要其他形式的随机数找java.util.Random 
		System.out.println("Math.random(): "+Math.random());
		System.out.println("<--------------------------------------->");
		//sqrt()求平方根
		System.out.println("Math.sqrt(4): "+Math.sqrt(4));
		System.out.println("Math.sqrt(-4): "+Math.sqrt(-4));
		System.out.println("<--------------------------------------->");
		//pow()求x的y次幂
		System.out.println("Math.pow(2,3): "+Math.pow(2,3));
		System.out.println("<--------------------------------------->");
		//sin(),cos();三角函数,这里只举两个例子,所有三角函数在Math中都有
		System.out.println("Math.sin(0): "+Math.sin(0));
		System.out.println("Math.cos(0): "+Math.cos(0));
		System.out.println("<--------------------------------------->");
	}
}



附带运行截图

在这里插入图片描述
在这里插入图片描述

来日方长,未来再见。



版权声明:本文为wangjunlei666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。