js转换成数字
2024-09-01 10:45:17
/**
* 转换成数字,有两种方法:
* 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
最新文章
- Windows 10 下mysql 安装后无法启动问题
- yii2.0 Activeform表单部分组件使用方法
- MyEclispe 2015 CI 15发布(附下载)
- 为什么接口类型可以直接new?
- bigworld源码分析(1)—— 研究bigworld的意义和目标
- Java动态替换InetAddress中DNS的做法简单分析2
- CustomProgressBar
- Text Template Transformation Toolkit
- sort merge join导致temp被爆菊
- iOS的推送机制APNs:本地推送&;远程推送
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
- reids配置参数详解
- Vuex- Action的 { commit }
- 记录使用CI框架开发项目时遇到的问题
- 2018DDCTF misc1
- 英语演讲稿——Get Along with Fear
- [转帖]linux tree命令--显示目录的树形结构
- UE4 二维相关
- http 2.0 新特性
- MYSQL 优化常用方法(转载)
热门文章
- proc - 进程信息伪文件系统
- 牛客小白月赛16 H小阳的贝壳 (线段树+差分数组)
- ltp-ddt nand_mtd_dd_rw_jffs2
- TCP协议之三次握手四次挥手
- Python3.5-20190526-廖老师-自我笔记-单元测试-参数换-paramunittest
- bzoj2560 串珠子 状压DP
- 【C++11新特性】 C++11智能指针之shared_ptr
- opencc模块用langconv替换
- window 安装VisualSvn Server
- Linux后台执行脚本 &;与nohup