JS中的数学计算<之简单实例讲解>

1.取余数   %

var a=10%3;

//a=1

 

2.取绝对值  Math.abs()

var a=Math.abs(-102.1);
var b=Math.abs(102.1);

//a=102.1;b=102.1

 

3.截取小数点后长度并进行四舍五入 toFixed()

复制代码
var num_1 = new Number(13.53);
alert(num_1.toFixed(1));
alert(num_1.toFixed(0));

//13.5
//14

var num_2 = new Number(13.56);
alert(num_2.toFixed(1));
alert(num_2.toFixed(0));

//13.6
//14
复制代码

 

4.取最小值 Math.min()

复制代码
Math.min(5,7) 
Math.min(-3,5) 
Math.min(-3,-5) 
(Math.min(7.25,7.30)

//结果
//5
//-3
//-5
//7.25
复制代码

 

5.取最大值  Math.max()

复制代码
Math.max(5,7,9) 
Math.max(-3,5) 
Math.max(-3,-5) 
Math.max(7.25,7.30)

//结果
//9
//5
//-3
//7.30
复制代码

 

6.指数计数法 和 截取数字固定长度 toPrecision()

复制代码
var a=1000.25;
alert(a.toPrecision(1))
alert(a.toPrecision(2))
alert(a.toPrecision(4))
alert(a.toPrecision(6))
alert(a.toPrecision(8))

//结果
//1e+3
//1.0e+3
//1000
//1000.25
//1000.2500
复制代码

 

7.把一个数字舍入为最接近的整数 round()

复制代码
Math.round(0.60) 
Math.round(0.50) 
Math.round(0.49)
Math.round(-4.40) 
Math.round(-4.60)

//结果
//1
//1
//0
//-4
//-5
复制代码

 

转载:http://www.cnblogs.com/zqzjs/p/4713139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值