码迷,mamicode.com
首页 > 其他好文 > 详细

Math对象

时间:2017-07-10 18:04:13      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:字符串   color   bsp   math   str   整数   dex   pre   span   

Math 对象

Math 对象用于执行数学任务。

结合保留两位小时的应用:

1,四舍五入

var num =2.446242342;
num = num.toFixed(2); // 输出结果为 2.45

2,非四舍五入:

第一种,先把小数边整数:

Math.floor(15.7784514000 * 100) / 100  
// 输出结果为 15.77

第二种,当作字符串,使用正则匹配:

Number(15.7784514000.toString().match(/^\d+(?:\.\d{0,2})?/))  
// 输出结果为 15.77,不能用于整数如 10 必须写为10.0000

 

Math.round(x) 四舍五入,如Math.round(0.60),结果为1;Math.round(0.49),结果为0;

Math.floor(x) 向下舍入,如Math.floor(0.60)与Math.floor(0.49),结果均为0;

Math.ceil(x)向上舍入,如Math.ceil(0.60)与Math.ceil(0. 49),结果均为1。

Math对象

标签:字符串   color   bsp   math   str   整数   dex   pre   span   

原文地址:http://www.cnblogs.com/wang715100018066/p/7146603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!