数学工具类Math

工具类Math

java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作

Math . PI 代表近似的圆周率常量(double)

案例:

public static void main(String[] args) {
//获取绝对值
System.out.println(Math.abs(3.14));//3.14
System.out.println(Math.abs(0));//0
System.out.println(Math.abs(-2.5));//-2.5

//向上取整
System.out.println(Math.ceil(3.9));//4.0
System.out.println(Math.ceil(3.1));//4.0
System.out.println(Math.ceil(3.0));//3.0

//向下取整,抹零
System.out.println(Math.floor(30.1));//30.0
System.out.println(Math.floor(30.9));//30.0
System.out.println(Math.floor(31.0));//31.0

//四舍五入
System.out.println(Math.round(20.4));//20
System.out.println(Math.round(10.5));//11
}

 

posted @   想见玺1面  阅读(65)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示