首页IT科技js中math.round(记录JS中的math.floor是什么意思?Math.round()/Math.ceil()/Math.floor()区别)

js中math.round(记录JS中的math.floor是什么意思?Math.round()/Math.ceil()/Math.floor()区别)

时间2025-05-05 17:19:00分类IT科技浏览3909
导读:今天老蒋有在用JS代码调整广告的随机显示的时候,看到有一段代码是用到math.floor,因为我之前有用到过Math.round,这个还是第一次遇到,也许之前有复制到没有细心看。这里简单的整理看看我们常用的Math.round( /Math.ceil( /Math.floor( 区别。...

今天老蒋有在用JS代码调整广告的随机显示的时候           ,看到有一段代码是用到math.floor                 ,因为我之前有用到过Math.round    ,这个还是第一次遇到        ,也许之前有复制到没有细心看           。这里简单的整理看看我们常用的Math.round()/Math.ceil()/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.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11

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

参考文档:https://www.cnblogs.com/johnsonwei/p/6101171.html

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
node.js怎么搭建服务器(如何通过nodejs快速搭建一个服务器)