Java中math常用函数

本文详细介绍了Java中Math类的各种数学函数及其用法,包括求绝对值、向上取整、向下取整、四舍五入、最大最小值、随机数、平方根、幂运算等,并通过示例展示了这些函数的具体应用。

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

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("<--------------------------------------->");
	}
}

附带运行截图

在这里插入图片描述在这里插入图片描述
来日方长,未来再见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值