数学工具类Math
Math.abs() 获取绝对值
Math.ceil() 向上取整
Math.floor() 向下取整
Math.round() 四舍五入
直接看代码:
public class MathTool {
public static void main(String[] args) {
//获取绝对值
System.out.println(Math.abs(-3.14));
System.out.println(Math.abs(-0.5555555555555));
System.out.println("======================");
//向上取整
System.out.println(Math.ceil(3.9));
System.out.println(Math.ceil(3.1));
System.out.println("======================");
//向下取整
System.out.println(Math.floor(321.9));
System.out.println(Math.floor(321.1));
System.out.println("======================");
//四舍五入
System.out.println(Math.round(3.44444));
System.out.println(Math.round(3.5555555555));
}
}
输出结果如下:

本文详细介绍了Java中数学工具类Math的基本用法,包括获取绝对值、向上取整、向下取整及四舍五入的方法,并通过具体示例代码展示了这些功能的实际应用。
862

被折叠的 条评论
为什么被折叠?



