JS中常用的Math方法
来源:互联网 发布:mac ps如何导入字体 编辑:程序博客网 时间:2024/06/04 19:58
1.min()和max()方法
Math.min()用于确定一组数值中的最小值。Math.max()用于确定一组数值中的最大值。
alert(Math.min(2,4,3,6,3,8,0,1,3)); //最小值alert(Math.max(4,7,8,3,1,9,6,0,3,2)); //最大值
2.舍入方法
Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数;
例如:
alert(Math.ceil(25.9)); //26alert(Math.ceil(25.5)); //26alert(Math.ceil(25.1)); //26 alert(Math.floor(25.9)); //25alert(Math.floor(25.5)); //25alert(Math.floor(25.1)); //25 alert(Math.round(25.9)); //26alert(Math.round(25.5)); //26alert(Math.round(25.1)); //25
3.random()方法
Math.random()方法返回介于0到1之间一个随机数,不包括0和1。如果想大于这个范围的话,可以套用一下公式:
值 = Math.floor(Math.random() * 总数 + 第一个值)
例如:
alert(Math.floor(Math.random() * 10 + 1)); //随机产生1-10之间的任意数
for (var i = 0; i<10;i ++) { document.write(Math.floor(Math.random() * 10 + 5)); //5-14之间的任意数 document.write('<br />');}
为了更加方便的传递想要范围,可以写成函数:
function selectFrom(lower, upper) { var sum = upper - lower + 1; //总数-第一个数+1 return Math.floor(Math.random() * sum + lower);} for (var i=0 ;i<10;i++) { document.write(selectFrom(5,10)); //直接传递范围即可 document.write('<br />');}
4.
Math 对象方法
转自:http://www.cnblogs.com/moqiutao/p/6277347.html
阅读全文
0 0
- JS中常用的Math方法
- JS中常用Math对象常用方法的整理
- js的Math常用方法
- cocos2d-js中Math对象的常用方法总结
- JS Math数学函数中一些常用的方法
- Math中常用的方法
- js中math常用方法总结
- js常用的Math对象的方法
- JS 中常用的Math 对象
- Math中常用方法
- JS中 Math方法
- Java中Math类的常用方法
- Java中Math类的常用方法
- Java中Math类的常用方法
- Java中Math类的常用方法
- Java中Math类的常用方法
- JAVA中Math 类的常用方法
- Java中Math类的常用方法
- LeetCode 11. Container With Most Water
- [日常训练] tty的方程
- ORB_SLAM2系列之三:ORB_SLAM2跑RGBD SLAM数据集
- 判断元素出栈、入栈顺序的合法性
- 《ActiveMQ in Action》【PDF】
- JS中常用的Math方法
- 矩阵链相乘
- Jfinal HttpKit.post(url,data)源码解析
- Winform中报表RDLC的设计与实现
- 6年前的防sql注入服务器安全配置文章,依然很有用
- [操作系统] 页面置换算法(一)
- 【Python】python引入外部文件
- HDU1251 统计难题(Trie树)
- 工作一周总结