Mandatory type conversion rules

Cast:
Character transfer value
parseInt (); a conversion from left to right, you can turn turn, can not be transferred stop; if you can not turn the first direct NaN; does not recognize decimal point.
parseFloat (); equivalent to parseInt, while the decimal can be identified

Math.round (); strict conversion, any non-numeric characters are not allowed, otherwise NaN; rounded to the nearest integer

Number (); strict conversion, any non-numeric characters are not allowed, otherwise NaN; direct conversion


var str = "123";
var str = "123abc";
var str = "123abc456";
var str = "a123";
var str = "adasd";
var str = "123.45";
var n = parseInt(str);
console.log(str);
console.log(typeof str);
console.log(n);
console.log(typeof n);

var str = "a567.892";
var n = parseFloat(str);
console.log(str);
console.log(typeof str);
console.log(n);
console.log(typeof n);

var str = "-456.789";
var n = Math.round(str);
console.log(str);
console.log(typeof str);
console.log(n);
console.log(typeof n);

var str = "-456.789a";
var n = Number(str);
console.log(str);
console.log(typeof str);
console.log(n);
console.log(typeof n);


Numerical character turn
toString (); direct conversion, to be converted to equivalent values quoted
retention n is a decimal
toFixed (); while quoted, rounded to n-bit fraction, not zero padding

var n = 10.3543;
var s = n.toString();
console.log(n);
console.log(typeof n);
console.log(s);
console.log(typeof s);

var n = 10;
var s = n.toFixed(2);
console.log(n);
console.log(typeof n);
console.log(s);
console.log(typeof s);

console.log(123.567000000)

Numerical character turn
var = n-123;
var n-S + = "";
the console.log (S)

Numerical characters turn
var S = "123";
var = n-S - 0;
the console.log (n-)

==================================

Other transfer value
to true 1, false is 0
the console.log (. 1 + true); // 2
the console.log (+. 1 to false); //. 1
the console.log (+ undefined. 1); // NaN3
the console.log ( + NaN3. 1); // NaN3
the console.log (+ null. 1); //. 1

Guess you like

Origin www.cnblogs.com/xiet/p/11747784.html
Recommended