大家好,又见面了,我是你们的朋友全栈君。
1、parseInt(number)
var
num1 = parseInt("2015nov"), //2015
num2 = parseInt(""), //NaN
num3 = parseInt("0xA"), //10(十六进制)
num4 = parseInt(20.15), //20
num5 = parseInt(-20.15), //-20
num6 = parseInt("070"); //56(八进制数)
2、~~number
//所有取整之中最快的,位运算,一如既往的快。
var num1 = ~~20.15, //20
num2 = ~~(-20.15); //-20
3、number^0
var num1 = 20.15^0, //20
num2 = (-20.15)^0; //-20
4、number<<0
var num1 = 20.15 << 0, //20
num2 = (-20.15) << 0, //-20
5、四舍五入Math.round(number)
var num1 = Math.round(20.1), //20
num2 = Math.round(20.5), //21
num3 = Math.round(20.9), //21
num4 = Math.round(-20.1), //-20
num5 = Math.round(-20.5), //-20 注意这里是-20而不是-21
num6 = Math.round(-20.9); //-21
6、向上取整Math.ceil(number)
var num1 = Math.ceil(20.1), //21
num2 = Math.ceil(20.5), //21
num3 = Math.ceil(20.9), //21
num4 = Math.ceil(-20.1), //-20
num5 = Math.ceil(-20.5), //-20
num6 = Math.ceil(-20.9); //-20
7、向下取整Math.floor(number)
var num1 = Math.floor(20.1), //20
num2 = Math.floor(20.5), //20
num3 = Math.floor(20.9), //20
num4 = Math.floor(-20.1), //-21
num5 = Math.floor(-20.5), //-21
num6 = Math.floor(-20.9), //-21
做个小笔记记录下方法,好记性不如烂笔头~~
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149338.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...