05-数据类型转换 – Python量化投资

05-数据类型转换

1.将数值类型转换成字符串类型                  

隐式转换                                                                            


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>流浪者</title>
 6 
 7 </head>
 8 <body>
 9 
10 <script>
11     var a = 123;
12     var b = '123';
13     console.log(typeof (a + b));  //数字和字符串相加,默认转换成字符串类型
14 </script>
15     
16 </body>
17 </html>


 

强制类型转换                                                       


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>流浪者</title>
 6 
 7 </head>
 8 <body>
 9 
10 <script>
11     var n = 123;
12     var str1 = String(n);
13     console.log(typeof str1);
14 
15     var str2 = n.toString();
16     console.log(typeof str2);
17 </script>
18     
19 </body>
20 </html>


2.将字符串类型转换成数值类型                


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>流浪者</title>
 6 
 7 </head>
 8 <body>
 9 
10 <script>
11     var stringNum = '755.24hffjgi';
12     var Num = Number(stringNum);
13     console.log(Num);
14     console.log(typeof Num);
15 
16     // parseInt()可以解析一个字符串,并返回一个整数
17     console.log(parseInt(stringNum));
18     console.log(parseFloat(stringNum));
19 </script>
20     
21 </body>
22 </html>


3.任何数据类型都可以转换成Boolean类型       


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>流浪者</title>
 6 
 7 </head>
 8 <body>
 9 
10 <script>
11     var a1 = '135';
12     var a2 = 0;
13     var a3 = -111;
14     var a4 = NaN;
15     var a5 = Infinity;
16     var a6;
17     var a7 = null;
18 
19     console.log(Boolean(a1));
20     console.log(Boolean(a2));
21     console.log(Boolean(a3));
22     console.log(Boolean(a4));
23     console.log(Boolean(a5));
24     console.log(Boolean(a6));
25     console.log(Boolean(a7));
26 </script>
27     
28 </body>
29 </html>


 

 

作者:流浪者

日期:2019-09-03

https://www.cnblogs.com/897463196-a/p/11454909.html

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论