JavaScript数据运算
栏目: JavaScript · 发布时间: 7年前
Math.pow(2,4) //16 2的4次幂
Math.ceil(.1) //1 向上(大)取值
Math.floor(.1) //0 向下(小)取值
Math.abs(-1) //1 绝对值|-1|
Math.max(1,2,3) //最大值
Math.min(1,2,3) //最小值
Math.random() //大于等于0小于1的伪随机数[1]
Math.round(.4) //四舍五入
Math.PI //3.141592653589793 π
Math.E //自然对数的底数2.718281828459045
Math.sqrt(9) //3 9的平方根 √9
Math.pow(27,1/3) //3 27的立方根 ³√27
Math.sin(x) //x 的正玄值。返回值在 -1.0 到 1.0 之间;必需。一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。
Math.cos(x) //x 的余弦值。返回的是 -1.0 到 1.0 之间的数;必须是一个数值。
Math.exp(3) //e的3次幂
复制代码
二、二进制浮点数和四舍五入的错误
var x=.3-.2; //x=0.09999999999999998 var y=.2-.1; //y=.1 x==y//false 复制代码
三、日期和时间
var then=new Date(2019,5,21) //Fri Jun 21 2019 00:00:00 2019年6月21日 var later=new Date(2019,5,21,10,14,59) //Fri(星期五) Jun(六月) 21 2019 10:14:59 GMT+0800 (中国标准时间) var now=new Date() //现在的时间日期 Tue May 21 2019 10:15:17 GMT+0800 (中国标准时间) now-then //日期的减法 毫秒 now.getFullYear() //=>2019 now.getMonth() //=>4(月份从0开始) now.getDate() //=>21 天数 now.getDay() //=>2星期数 星期天是0 now.getHours() //=>10 小时 now.getMinutes() //=>15 分钟 now.getSeconds() //=>40 秒 now.getUTCHours() //=>UTC时间2点, 北京时间比UTC时间早8个小时 复制代码
四、字符串
var msg='sheng,xia,wan,qiu';
msg.charAt(0) //=>"s"第一个字符
msg.charAt(msg.length-1) //=>"u"最后一个字符
msg.substring(1,5) //=>"heng"第2到第5个字符
msg.slice(1,5) //=>"heng"第2到第5个字符
msg.slice(-3) //=>"qiu"最后3个字符
msg.indexOf('i') //=>7第一次出现i的位置
msg.lastIndexOf('i') //=>15最后一次出现i的位置
msg.indexOf('i',8) //=>15在位置8或8之后首次出现i的位置
msg.split(',') //=>["sheng", "xia", "wan", "qiu"]根据‘,’分割
msg.replace('s','S') //=>"Sheng,xia,wan,qiu"全文字符串替换
msg.toUpperCase() //=>"SHENG,XIA,WAN,QIU"
复制代码
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Cyberwar
Kathleen Hall Jamieson / Oxford University Press / 2018-10-3 / USD 16.96
The question of how Donald Trump won the 2016 election looms over his presidency. In particular, were the 78,000 voters who gave him an Electoral College victory affected by the Russian trolls and hac......一起来看看 《Cyberwar》 这本书的介绍吧!
XML 在线格式化
在线 XML 格式化压缩工具
HEX CMYK 转换工具
HEX CMYK 互转工具