To allow more flexible conversions, you can use parseInt( ) and parseFloat( ). These functions convert and return any number
at the beginning of a string, ignoring any trailing non-numbers. parseInt( ) parses only integers, while parseFloat( ) parses both integers and floating-point numbers. If a string begins with "0x" or "0X", parseInt( ) interprets it as a hexadecimal number.[4] For example:
[4] The ECMAScript specification says that if a string begins with "0" (but not "0x" or "0X"), parseInt( ) may parse it as an octal number or as a decimal number. Because the behavior is unspecified, you should never use parseInt( ) to parse numbers with leading zeros, unless you explicitly specify the radix to be used!
parseInt("3 blind mice"); // Returns 3
parseFloat("3.14 meters"); // Returns 3.14
parseInt("12.34"); // Returns 12
parseInt("0xFF"); // Returns 255
parseInt( ) can even take a second argument specifying the radix (base) of the number to be parsed. Legal values are between 2 and 36. For example:
parseInt("11", 2); // Returns 3 (1*2 + 1)
parseInt("ff", 16); // Returns 255 (15*16 + 15)
parseInt("zz", 36); // Returns 1295 (35*36 + 35)
parseInt("077", 8); // Returns 63 (7*8 + 7)
parseInt("077", 10); // Returns 77 (7*10 + 7)
If parseInt( ) or parseFloat( ) cannot convert the specified string to a number, it returns NaN:
parseInt("eleven"); // Returns NaN
parseFloat("$72.47"); // Returns NaN
分享到:
相关推荐
Converting Array to Map in Java
Converting DIB to DDB将一个设备无关的位图转换成设备相关的位图(5KB)
Converting numbers to strings and strings to numbers, How To: Ask Questions The Smart Way ), Forum( 说明:forum部分由于内容太多,体积太大(包括总体积27M,forum部分就占9.5M),而且仅仅是论坛中的讨论而已...
Converting DDB to DIB将一个设备相关的位图转换成设备无关的位图(6KB)
Converting the CONNECT sample to a local server在本地服务器实现连接的例子(6KB)
提供一种将相位噪声转化为时域抖动的方法。
通过简单的GDB的使用,来考察 1、考察C语言的main函数的参数列表*argv[]的特性。 2、考察C语言的二级指针的使用。(pointers arrays;pointers to pointers)
D3D to opengl converting library
ffmpeg mp3 转换命令
Data: Numbers and Strings Chapter 4. Objects and Prototypes Chapter 5. Arrays Chapter 6. Functions Chapter 7. From Signs to Patterns Chapter 8. JavaScript in the Web Page Chapter 9. Graphic and ...
Converting SIDs Between Strings and Binary
Converting a bitmap to a region - memory leak fix将位图转化为一个区域 - 修补了内存漏洞(179KB)
ADI工程师Walt Kester的Converting Oscillator Phase Noise to Time Jitter,讲述如何通过PLL相噪计算得到jitter
AB PLC例程_本资料仅供学习参考
Converting a bitmap to a region - memory leak fix将一个位图转换成一个区域--内存泄露的修正(4KB)
converting-from-speech-to-text-with-javascript 本教程中,我们将尝试使用Web Speech API,这是一个非常强大的浏览器接口,可以用来记录语音并将其转换为文本,同样的,也可以用来朗读字符串。 接下来进入正题,这...
Converting a bitmap to a region将一个位图转换成一个区域(7KB)
Bytecode to source converting Java & Kotlin code into
float类型的表示可以有以下几种: 复制代码 代码如下: <?... $b = 1.2e3; $c = 7E-10; ?>...还有就是不要试图进行比较... Converting to float For information on converting strings to float, see String conver
此函数根据由协方差矩阵的特征值的平方根组成的输入计算球面误差可能半径(等效地,来自坐标系中三变量正态分布的 sigma-x、sigma-y 和 sigma-z,其中存在变量之间没有互相关。)这意味着如果您有一个协方差矩阵并...