java的取整方法

1.round():数学上的“四舍五入”法。

public class Test {
	public static void main(String[] args) {
		System.out.println(Math.round(11.5));
		System.out.println(Math.round(-11.5));
	}
}

运行结果分别为12和-11。
2.ceil():向上取整。

public class Test {
	public static void main(String[] args) {
		System.out.println(Math.ceil(2.36));
		System.out.println(Math.ceil(3.5));
		System.out.println(Math.ceil(-6.3));
	}
}

运行结果分别为3.0、4.0、-6.0。
3.floor():向下取整。

public class Test {
	public static void main(String[] args) {
		System.out.println(Math.floor(2.36));
		System.out.println(Math.floor(3.5));
		System.out.println(Math.floor(-6.3));
	}
}

运行结果分别为2.0、3.0、-7.0。

发布了81 篇原创文章 · 获赞 22 · 访问量 7659

猜你喜欢

转载自blog.csdn.net/qq_38883271/article/details/104099458