当前位置:网站首页>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 :
1、parseInt() String to integer ,parseFloat() Convert string to floating point ,Number() Convert string to numeric ;
2、
Number(): 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>
边栏推荐
- 【obs】视频、音频编码与rtmp发送的配合
- JS数组方法 sort() 排序规则解析
- Introduction and use of Pinia
- Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research
- Maximum sum and promotion of continuous subarrays (2)
- [verification] only numbers (positive and negative numbers) can be entered
- 下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
- jmeter -- prometheus+grafana服务器性能可视化
- 2022最新短视频去水印解析API接口分享
- The collapse of margin
猜你喜欢

Go language interface and type

Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t

In depth analysis of the famous Alibaba cloud log4j vulnerability

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单

Get familiar with pytoch and pytoch environment configuration

0630~ professional quality course

0625~<config>-<bus>
去不图床容量兑换

Use of jumpserver

Three ways of redis cluster
随机推荐
继承与派生
Just one dependency to give swagger a new skin, which is simple and cool!
Number of times a number appears in an ascending array
Example of single table query in ORM student management system
[verification] only numbers (positive and negative numbers) can be entered
0630~ professional quality course
如何为超级通胀做好准备
Simple test JS code
Common methods of number and math classes
【校验】只能输入数字(正负数)
Use of jumpserver
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
Blackmagic Fusion Studio 18
Ship new idea 2022.2 was officially released, and the new features are really fragrant!
Laravel notes - RSA encryption of user login password (improve system security)
Framework introduction
Model saving and loading of sklearn
0627~ holiday knowledge summary
字符串常用方法(2)
Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t