Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
86
3
console.log(parseFloat(' ')); // NaN
3.5.3
讨论
开发人员会使用一些技巧,实现与
Number()
函数等同的功能,例如在字符串上乘
1
numberInString*1
),或者使用一元运算符(
+numberInString
)。但是,
为了明确表明意图,建议使用
Number()
parseFloat()
如果数字带格式(例如
2,300
),工作量要大一些
Number()
函数返回
NaN
parseFloat()
方法在逗号处停止,得到
2
。虽然
JavaScript
供了
Intl.
NumberFormat
对象,可以根据数字创建带格式的字符串(见
2.2
节),但是可惜,
没有提供反向操作的解析功能。
为了把字符串中的逗号去掉,可以使用正则表达式(见
2.8
节)。但是,自己实现
解决方案有一定的风险,因为有些区域使用逗号分隔千位,还有些区域使用逗号分
隔小数位。在这种情况下,最好使用经过大众检验的
JavaScript
库,例如
Numeral
http://numeraljs.com
)。
3.6
把十进制转换成十六进制
3.6.1
问题
有一个十进制值,你想找出等效的十六进制形式。
3.6.2
方案
使用
Number.toString()
法,通过参数指定转换目标的基数。
const
num = 255;
//
显示
ff
,即与
255
等效的十六进制形式
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript

JavaScript

T. J. Crowder
JavaScript

JavaScript

David Flanagan
JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov

Publisher Resources

ISBN: 9787519869601