JavaScript:Converting Strings to Numbers

We've seen that strings that represent numbers are automatically converted to actual numbers when used in a numeric context. We can make this conversion explicit by choosing the numeric context we use. Just as we can convert a number to a string by adding the empty string to it, we can convert a string to a number by subtracting zero from it:

numeric_value = string_value - 0;

We can't add zero, of course, because in that case the + operator would be interpreted as the string concatenation operator.
The trouble with this sort of string-to-number conversion is that it is overly strict. It works only with base-10 numbers, and only when the string contains nothing but leading spaces and numbers, with no trailing characters, not even trailing spaces. To allow more flexible conversions, you can use the parseInt() and parseFloat() functions. These convert and return any number at the beginning of a string, ignoring any trailing non-numbers. parseInt() only parses integers, and parseFloat() parses both integers and floating-point numbers. If a number begins with 0, parseInt() interprets it as an octal number. If it begins with 0x or 0X, parseInt() interprets it as a hexadecimal number.


parseInt("3 blind mice");      // returns 3
parseFloat("3.14 meters");     // returns 3.14
parseInt("12.34");             // returns 12
parseInt("077");               // returns 63 (7*8 + 7)
parseInt("0xFF");              // returns 255

parseInt() can even take a second argument, which specifies 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)

If parseInt() or parseFloat() cannot convert the specified string to a number, they return NaN in Navigator 3.0 (and on Unix platforms in Navigator 2.0). On Navigator 2.0 non-Unix platforms and in Internet Explorer 3.0, these functions return 0 in this case, which makes it impossible to distinguish between the legal string "0" and an a string that does not represent a number. A future version of IE will correctly support the NaN return value.

parseInt("eleven");            // returns NaN (or 0)
parseFloat("$72.47");          // returns NaN (or 0)

Finally, you can also convert strings to numbers (and to other types) with the eval() method. This method interprets an arbitrary JavaScript expression and returns the result (which may be of any JavaScript type). For example:


eval("3.14");                  // returns 3.14
eval("2 * 3.14 * radius");     // returns the result of the multiplication
eval("radius > 3");            // returns true or false

Note that you rarely actually need to use eval()--generally, your JavaScript expressions occur in JavaScript code itself, not in strings that are later evaluated!
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值