Math.ceil,Math.round,Math.floor三者的区别

1.Math.round():根据“round”的字面意思“附近、周围”,可以猜测该函数是求一个附近的整数,看下面几个例子就明白。

小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11
 
小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12
 
小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11
总结:(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。
 
2.Math.ceil():根据“ceil”的字面意思“天花板”去理解;
例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
 
3.Math.floor():根据“floor”的字面意思“地板”去理解;
例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=11

Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-12



double a = 0.1;
        double b = 0.50;
        double c = 0;
        double d = 0.4;
        double e = 0.6;
        double f = 1.3;
        double g = 1.6;

        //Math.ceil 表示的是大于某个小数的最小整数的double值;  ceiling最高限度 天花板
        double ceil1 = Math.ceil(a);
        double ceil2 = Math.ceil(b);
        double ceil3 = Math.ceil(c);
        double ceil4 = Math.ceil(d);
        double ceil5 = Math.ceil(e);

        //Math.round  表示的是四舍五入,且得到long值
        long round1 = Math.round(a);
        long round2 = Math.round(b);
        long round3 = Math.round(c);
        long round4 = Math.round(d);
        long round5 = Math.round(e);

        //Math.floor 跟Math.ceil相反,表示的是小于某个小数的最大整数值的double类型; floor底板 最底限度
        double floor1 = Math.floor(a);
        double floor2 = Math.floor(b);
        double floor3 = Math.floor(c);
        double floor4 = Math.floor(d);
        double floor5 = Math.floor(e);
        double floor6 = Math.floor(f);
        double floor7 = Math.floor(g);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

这里写图片描述


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值