微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐