栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript字符串和数字转换

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript字符串和数字转换

您想熟悉

parseInt()
toString()

在您的工具箱中有用的是查看变量以找出它是什么类型的

typeof

<script type="text/javascript">        function printWithType(val) {        document.write('<pre>');        document.write(val);        document.write(' ');        document.writeln(typeof val);        document.write('</pre>');    }    var a = "1", b = "2", c = "3", result;    // Step (1) Concatenate "1", "2", "3" into "123"    // - concatenation operator is just "+", as long    //   as all the items are strings, this works    result = a + b + c;    printWithType(result); //123 string    // - If they were not strings you could do    result = a.toString() + b.toString() + c.toString();    printWithType(result); // 123 string    // Step (2) Convert "123" into 123    result = parseInt(result,10);    printWithType(result); // 123 number    // Step (3) Add 123 + 100 = 223    result = result + 100;    printWithType(result); // 223 number    // Step (4) Convert 223 into "223"    result = result.toString(); //    printWithType(result); // 223 string    // If you concatenate a number with a     // blank string, you get a string        result = result + "";    printWithType(result); //223 string</script>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442295.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号