/**
* 转换成数字,有两种方法:
* 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

最新文章

  1. Windows 10 下mysql 安装后无法启动问题
  2. yii2.0 Activeform表单部分组件使用方法
  3. MyEclispe 2015 CI 15发布(附下载)
  4. 为什么接口类型可以直接new?
  5. bigworld源码分析(1)—— 研究bigworld的意义和目标
  6. Java动态替换InetAddress中DNS的做法简单分析2
  7. CustomProgressBar
  8. Text Template Transformation Toolkit
  9. sort merge join导致temp被爆菊
  10. iOS的推送机制APNs:本地推送&远程推送
  11. javaweb学习总结(七)——HttpServletResponse对象(一)(转)
  12. reids配置参数详解
  13. Vuex- Action的 { commit }
  14. 记录使用CI框架开发项目时遇到的问题
  15. 2018DDCTF misc1
  16. 英语演讲稿——Get Along with Fear
  17. [转帖]linux tree命令--显示目录的树形结构
  18. UE4 二维相关
  19. http 2.0 新特性
  20. MYSQL 优化常用方法(转载)

热门文章

  1. proc - 进程信息伪文件系统
  2. 牛客小白月赛16 H小阳的贝壳 (线段树+差分数组)
  3. ltp-ddt nand_mtd_dd_rw_jffs2
  4. TCP协议之三次握手四次挥手
  5. Python3.5-20190526-廖老师-自我笔记-单元测试-参数换-paramunittest
  6. bzoj2560 串珠子 状压DP
  7. 【C++11新特性】 C++11智能指针之shared_ptr
  8. opencc模块用langconv替换
  9. window 安装VisualSvn Server
  10. Linux后台执行脚本 &与nohup