首页 > 编程知识 正文

js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]...

时间:2023-05-03 15:42:28 阅读:273858 作者:3717

<script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write('<br>3.5的四舍五入:'+Math.round(3.01)); write('<br>3.01的进一取整:'+Math.ceil(3.01)); write('<br>3.99的舍去取整:'+Math.floor(3.99)); write('<br>获取最大值:'+Math.max(10,20,45,12)); write('<br>获取最大值:'+Math.min(10,20,45,12)); write('<br>获取圆周率'+Math.PI); // Math.random():获取的>=0 <1的随机数 write('<br>获取随机数'+Math.random()); /* // 0-10的随机数 Math.round(Math.random()*10); 0-0.5 0 0.5-1.5 1 1.5-2.5 2 9.5-10 10 */ // 0 - 10的随机数 console.log(Math.ceil(Math.random()*100000)%11); // 10-50的随机数 /* 0-40 +10 */ console.log(Math.ceil(Math.random()*100000)%41+10); // m-n的随机数 function getRandom(m, n){ return Math.ceil(Math.random()*100000)%(n-m+1)+m; } console.log(getRandom(2,3)); } </script>

 

转载于:https://www.cnblogs.com/jkr666666/p/6964860.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。