Math的几种常用方法,向上取整,向下取整,四舍五入,正弦 余弦

229 阅读1分钟

S中有个全局对象 用于普通的计算 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的正弦值

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