java中的Math.ceil、Math.floor和Math.round
发布时间:2020-12-15 07:32:40 所属栏目:Java 来源:网络整理
导读:ceil意为天花板,指向上取整;floor意为地板,指向下取整;round指四舍五入 package com.company; public class Main { public static void main(String[] args) { // 向上取整 System.out.println(Math.ceil(11.3)); // 12.0 System.out.println(Math.ceil(
ceil意为天花板,指向上取整;floor意为地板,指向下取整;round指四舍五入 package com.company; public class Main { public static void main(String[] args) { //向上取整 System.out.println(Math.ceil(11.3));//12.0 System.out.println(Math.ceil(-11.3));//-11.0 //向下取整 System.out.println(Math.floor(11.3));//11.0 System.out.println(Math.floor(-11.3));//-12.0 //四舍五入 算法为Math.floor(x+0.5) 即原来的数字加上0.5再向下取整 System.out.println(Math.round(11.4));//11 System.out.println(Math.round(11.5));//12 System.out.println(Math.round(11.6));//12 System.out.println(Math.round(-11.4));//-11 System.out.println(Math.round(-11.5));//-11 System.out.println(Math.round(-11.6));//-12 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |