JS中数学对象Math(易懂)

编程入门 行业动态 更新时间:2024-10-25 14:25:07

JS中数学对象Math(<a href=https://www.elefans.com/category/jswz/34/1769350.html style=易懂)"/>

JS中数学对象Math(易懂)

一提到数学,就想到被数学支配的噩梦,只不过这个数学用在了代码当中,那么代码当中的数学对象又是什么样的呢?让我为大家简单介绍一下吧!
数学对象常用方法:

常用方法简述
ceil向上取整
floor向下取整
round四舍五入
max找最大值
min找最小值
random生成 0 ~ 1 之间的随机数( 包括0不包括1 )
    // 方法// ceil 向上取整console.log(Math.ceil(1.1)) // 2console.log(Math.ceil(1.5)) // 2console.log(Math.ceil(1.9)) // 2// floor 向下取整console.log(Math.floor(1.1)) // 1console.log(Math.floor(1.5)) // 1console.log(Math.floor(1.9)) // 1// round 四舍五入console.log(Math.round(1.1)) // 1console.log(Math.round(1.5)) // 2console.log(Math.round(1.9)) // 2// max 最大值console.log(Math.max(1,2,3,4,5)) // 5// min 最小值console.log(Math.min(1,2,3,4,5)) // 1// abs 绝对值console.log(Math.abs(-1)) // 1 // random 生成随机数// 左闭右开 能取到0 但取不到1 中间的一个随机小数console.log(Math.random())

如何生成0 - 10 之间的整数随机数

Math.floor(Math.random() * (10 + 1))

如何生成5 - 10之间的整数随机数

Math.floor(Math.random() * (5 + 1))+5

如何生成N - M之间的整数随机数

Math.floor(Math.random() * (M - N + 1)) + N

感谢大家的阅读,本人文笔有限,如有问题,可以向我提出,感谢大家!

更多推荐

JS中数学对象Math(易懂)

本文发布于:2023-12-06 23:22:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1669315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:易懂   对象   数学   JS   Math

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!