js四舍五入取整(Math的常用方法)

精英怪
广告

JS中有个全局对象 用于普通的计算 Math

js四舍五入取整(Math的常用方法)

它有一些方法,用于平时的计算,这里详细介绍几个常用的

Math.floor(x) // 返回小于x的最大整数

Math.floor(12.2) // 12

Math.floor(15 / 2) // 7

Math.ceil(x) // 返回大于x的最小整数

Math.ceil(12.2) // 13

Math.ceil(15 / 2) // 8

Math.round() 返回四舍五入后的整数

Math.round(12.2) // 12

Math.round(15 / 2) // 8

Math.random() 返回0到1之间的随机数.

Math.cos(x) 返回x的余弦值

Math.sin(x) 返回x的正弦值

写文章不容易,如果对你有帮助请点个赞吧。有时间的可以评论一下。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,277人围观)

还没有评论,来说两句吧...