博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS中的数学计算<之简单实例讲解>
阅读量:7051 次
发布时间:2019-06-28

本文共 859 字,大约阅读时间需要 2 分钟。

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//14var 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://jwpol.baihongyu.com/

你可能感兴趣的文章
linux source命令的用法
查看>>
微信小程序把玩(二十六)navigator组件
查看>>
Visual Studio 2017正式版发布全纪录
查看>>
1520-win10
查看>>
thinkjs——moment.js之前后台引入问题
查看>>
Java 线程内异常处理
查看>>
二:vlan,gre,vxlan
查看>>
静态内部类和非静态内部类的区别
查看>>
C语言 · 栅格打印问题
查看>>
【mysql】备份篇2:使用java程序定期备份mysql数据库
查看>>
BZOJ 4514: [Sdoi2016]数字配对 [费用流 数论]
查看>>
mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法
查看>>
Tooltip浮动提示框效果(掌握里面的小知识)
查看>>
getline函数(精华版)
查看>>
myeclipse debug不显示变量值解决的方法
查看>>
Cygwin-Cygwin ssh Connection closed by ::1 出错
查看>>
SpringMVC工作原理
查看>>
【NLP】文本相似度
查看>>
python模拟Get请求保存网易歌曲的url
查看>>
Gson 解析教程
查看>>