java中的Math.ceil、Math.floor和Math.round

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
    }
}

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...