首页 技术 正文
技术 2022年11月15日
0 收藏 992 点赞 3,312 浏览 1704 个字
/**
* 转换成数字,有两种方法:
* 1.Number(var)
* 2.parseInt(var)与parseFloat(var)
*/
// 字符串转换成数字
// 纯字母
var foo = 'hello';
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): NaN
// 字母数字混合
var foo = 'hello123';
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): NaN
// 纯数字
var foo = '8848';
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 8848
var foo = '3.14234';
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 3.14234
// boolean
var foo = true;
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 1
var foo = false;
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 0
// null
var foo = null;
console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 0
// undefined
var foo = undefined;
console.log(`typeof foo: ${typeof foo}, Number(foo): ${Number(foo)}`); // typeof foo: undefined, Number(foo): NaN
// 字母数字混合
// 字母在前
var foo = 'hello123';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN
// 字母在后
var foo = '123hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 123
// 穿插
var foo = '13word23hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 13
var foo = 'w13ord23hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN
// 夹带浮点数
var foo = 'hello23.54';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN
var foo = '23.54hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 23
var foo = '0.999hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 0
var foo = 'wor0.999hello';
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN
var foo = '070';
// parseInt默认十进制
console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 70
// 八进制
console.log(`parseInt(foo, 8): ${parseInt(foo, 8)}`); // parseInt(foo): 56
// 十六进制
console.log(`parseInt(foo, 16): ${parseInt(foo, 16)}`); // parseInt(foo): 122
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844