当前位置:网站首页>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>
边栏推荐
- [verification] only numbers (positive and negative numbers) can be entered
- Model saving and loading of sklearn
- Still reading logs on the command line? Use kibana, visual log analysis yyds!
- Common methods of number and math classes
- ["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
- Still building projects from scratch? This upgraded rapid development scaffold is worth a try!
- Cookies and session "suggestions collection"
- The collapse of margin
- [record of question brushing] 20. Valid brackets
- odoo中的bom理解
猜你喜欢

Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它

初识Pytorch和Pytorch环境配置

Inherit, override, overload

pycharm配置opencv库

In depth analysis of the famous Alibaba cloud log4j vulnerability

下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑

Win10 super good-looking mouse theme, you also try it

How to render millions of 2D objects smoothly with webgpu?

Number of times a number appears in an ascending array
![[OBS] dependency Library: x264 vs Build](/img/24/4caea5dc6ff092a3161f43b6026d25.png)
[OBS] dependency Library: x264 vs Build
随机推荐
How to quickly upload files to Google Lab
JS数组方法 sort() 排序规则解析
Inherit, override, overload
0611~ self study class
BOM understanding in odoo
如何用WebGPU流畅渲染百万级2D物体?
数组扁平化.flat(Infinity)
[record of question brushing] 20. Valid brackets
字符串常用方法(2)
Go to bed capacity exchange
[verification] only numbers (positive and negative numbers) can be entered
jmeter -- prometheus+grafana服务器性能可视化
Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research
redis集群的三种方式
pycharm配置opencv库
Highcharts chart and report display, export data
Section 7 Data Dictionary: hash followed by Daewoo redis ------- directory post
数组对象方法 常用遍历方法&高阶函数
middleware
Common methods of string (2)