当前位置:网站首页>2. JS variable type conversion, automatic conversion, manual conversion, what is the difference between parseint(), parsefloat(), number()?

2. JS variable type conversion, automatic conversion, manual conversion, what is the difference between parseint(), parsefloat(), number()?

2022-07-24 18:21:00 yingxingyf

2.js Variable type conversion 、 Automatic conversion 、 Manual switching 、 Excuse me, parseInt(),parseFloat(),Number() The difference between ?

1) Implicit ( Automatically ) transformation , When different data types participate in the expression operation process, they will be converted to the same type for operation 
 Implicit conversion rules for string and numeric type operations :
	1. String plus number , The number will be converted into a string .
	2. Number minus string , Turn a string into a number . If the string is not a pure number, it will be converted to NaN. String minus number is the same . two 
	 The subtraction of a string is first converted to a number .
	3. ride , except , Greater than , The same is true for the conversion of less than and minus .
	
2) Show ( Manual ) transformation 
	 String to value :
	parseInt()parseFloat()Number()
	 Value to string :
	toString()
 notes :NaN It's not a number , But it is still a numeric type , not a number,NaN yes Number type .
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
	</head>
	<body>
		
	</body>
</html>
<script type="text/javascript">
//js Data type conversion :  When operations occur between different data types , You need to convert data types first , And then calculate .
//1、 Automatic conversion 
//1)、 +
/*
var str=" Age is :"+ 20;// The first 20 Convert to string type , And then we'll do the stitching 
alert(str);
*/
//2)、-
//var str="25" - 20;// The first "25" Turn to number type , Then subtract ;
//var str="ab"-20; // The first "ab" Turn to number type , Then subtract ; because "ab" Can't be converted to numbers , That's it NaN(not a number)
//alert(str);

//3)、 ride , except , Greater than , The same is true for the conversion of less than and minus .

//2、 Manual switching 
//1)、 Turn a string into a number 
// parseInt();// Convert a string to an integer 
// parseFloat();// Convert string to floating point ( decimal )
// number();// Turn a string into a number 

//parseInt():
// Conversion rules : Start with the first character of the string ,
// If the first character is non numeric , So the result is NaN;
//  If the first character is a number , Then keep looking back , Until you encounter the first non number or the end . Turn all the previous contents into numbers .
/*
//var str="250";//250
//var str="250.61.8";//250
//var str="250.612a.8";//250
//var str="a250.612a.8";//NaN

var num = parseInt(str);// hold str To integer ;
alert(typeof num);
alert(num);
*/

//parseFloat():
// Conversion rules : Follow parseInt Close to ;
// Start with the first character of the string ,
// If the first character is non numeric , So the result is NaN;
//  If the first character is a number , Then keep looking back ,
//  Encounter the first decimal point , Then normal conversion ,
//  Meet the second decimal point or other non numeric or ending . Turn all the previous contents into numbers .

//var str="250.6";//250.6
//var str="250.61.8";//250.61
//var str="250.612a.8";//250.612

//var str="a250.612a.8";//NaN
/*
var num = parseFloat(str);
alert(typeof num);
alert(num);
*/

//Number():
//Number() The rules of transformation :
//Number() Look at the whole , As long as the content in the string is not a legal number , It would be NaN;

//var str="250.6";
//var str="250";   
//var str="250.61.8";//NaN
//var str="250.612a.8";//NaN
//var str="a250.612a.8";//NaN

/*
var num = Number(str);
alert(typeof num);
alert(num);
*/

//2)toString()

var age = 12;
var str = age.toString();
alert(typeof str);


/*----------------------------------- Interview questions --------------------------------------------*/
/*
// Interview questions : Excuse me, parseInt(),parseFloat(),Number() The difference between ?
// answer :
  1parseInt() String to integer ,parseFloat() Convert string to floating point ,Number() Convert string to numeric ;
  2Number(): Look at the whole , As long as the content in the string is not a legal number , The result is NaN; otherwise , It will be converted to numeric type normally ( Integer or floating point ).
  parseInt() and parseFloat() The conversion rules of are similar ( similar );
   Front to back , If the first character is not a number , that , The result is NaN; If the first character is a number ( Keep looking back ):
   1)、parseInt(): If you encounter a decimal point or other non numeric characters or endings , Then convert the previous content into numbers 
   2)、parseFloat(): If you encounter the second decimal point or other non numeric characters or endings , Then convert the previous content into numbers 

  */ 


</script>
原网站

版权声明
本文为[yingxingyf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/203/202207211137499156.html