isNaN
isNaN = function() {
return Number(arguments[0]).toString()==="NaN"
//注意,NaN===NaN //false
}
console.log(isNaN("")) //0
console.log(isNaN("3")) //3
console.log(isNaN(0)) //0
console.log(isNaN(-0)) //-0
console.log(isNaN(null)) //0 注意
console.log(isNaN(false)) //0
console.log(isNaN(undefined)) //NaN
console.log(isNaN(NaN)) //NaN
console.log(isNaN("ss")) //NaN
console.log(isNaN({})) //NaN
console.log(isNaN({name:"ss"})) //NaN
console.log(isNaN([])) //0 注意
console.log(isNaN([0])) //0 注意
console.log(isNaN([33])) //33 注意
console.log(isNaN([1,2])) //NaN
console.log(isNaN(new Number(2)) ) //2
console.log(isNaN(new String("2")) ) //2
parseFloat
// 总结:基础类型除了0,都返回NaN,对象也是,但数组先看第一项,在转
let num = 10;
parseFloat = function () {
let type = Object.prototype.toString.call(arguments[0])
if (type === "[object Number]") {
return arguments[0]
} else if (arguments[0]&&type === "[object String]") {
// 1.正则
let str = arguments[0].trim()
let matchNum = str.match(/^-?\d+(\.\d+)?/g)?str.match(/^-?\d+(\.\d+)?/g)[0]:NaN
return matchNum
//2.字符类型判断
// for (let i = 0; i < arguments[0].length; i++) {
// }
} else if (Array.isArray(arguments[0])) {
return arguments.callee(arguments[0][0])
} else {
return NaN
}
}
console.log(parseFloat(" 12.777qewqewq11")) //12.777
parseInt
// 总结:基础类型除了0,都返回NaN,对象也是,但数组先看第一项,在转
let num = 10;
parseInt = function () {
let type = Object.prototype.toString.call(arguments[0])
if (type === "[object Number]") {
return arguments[0]
} else if (arguments[0]&&type === "[object String]") {
// 1.正则
let str = arguments[0].trim()
let matchNum = str.match(/^-?\d+/g)?str.match(/^-?\d*/g)[0]:NaN
return matchNum
//2.字符类型判断
// for (let i = 0; i < arguments[0].length; i++) {
// }
} else if (Array.isArray(arguments[0])) {
return arguments.callee(arguments[0][0])
} else {
return NaN
}
}
console.log(parseInt(" 12qewq11")) //12
console.log(parseInt("")) //NaN,注意
console.log(parseInt("0")) //0,注意
console.log(parseInt(0)) //0
console.log(parseInt(-0)) //0,注意
console.log(parseInt(null)) //NaN
console.log(parseInt(undefined)) //NaN
console.log(parseInt(NaN)) //NaN
console.log(parseInt(false)) //NaN
console.log(parseInt("ss")) //NaN
console.log(parseInt({})) //NaN
console.log(parseInt({ name: "ss" })) //NaN
console.log(parseInt([])) //NaN
console.log(parseInt([0])) //0,注意,将数组转为字符串,以逗号分隔
console.log(parseInt([1, 2])) //1
console.log(parseInt(new Number(2))) //2
console.log(parseInt(" 23ewq")) //23 //有左trim
console.log(parseInt([" ", "", 1, 23, "4", "qew", 5])) //NaN
console.log(parseInt(["", 1, 23, "4", "qew", 5])) //NaN
console.log(parseInt(["-8"])) //-8
console.log(parseInt([0])) // 0
console.log(parseInt([10])) //10
console.log(parseInt([10, 12])) //10
console.log(parseInt([-6, 12])) //-6
console.log(parseInt([[[" 12ewqewq"]]])) //12
toPrecision
Number.prototype.toPrecision = function (length) {
if (Object.prototype.toString.call(length) === "[object Number]") {
if (this.toString().indexOf(".") > -1) {
return Number(this.toString().slice(0, length + 1))
} else {
return Number(this.toString().slice(0, length))
}
} else {
throw new Error("必须为数字")
}
}
console.log(new Number(101.78458).toPrecision(new Number(5)))
toFixed
// 返回指定小数位数的表示形式
Number.prototype.toFixed = function(length) {
if (Object.prototype.toString.call(length) === "[object Number]"&&!isNaN(length)) {
let numStr = this.toString()
let numLength = numStr.length
let pointIndex = numStr.indexOf(".")
if(pointIndex>-1){
if(numLength-pointIndex-1>=length){
return numStr.slice(0,pointIndex+1+length)
}else{
for(let i=0;i<length-numLength+pointIndex+1;i++){
numStr+="0"
}
return numStr
}
}else{
numStr +="."
for(let i=0;i<length;i++){
numStr+="0"
}
return numStr
}
} else {
throw new Error("必须为数字")
}
}
let a = 12
let b = 12.1
let c = 12.1234567
console.log(a.toFixed(3))
console.log(b.toFixed(3))
console.log(c.toFixed(6))