[thelist] javascript parseInt (TIP)
liorean
liorean at gmail.com
Sat Sep 16 08:19:30 CDT 2006
<tip type="ECMAScript string-to-number conversions">Here is a table
displaying the different ways to convert a string to a number.
n Number(n) parseInt(n) parseInt(n,10) parseFloat(n)
isNaN(n) eval(n)
---- ---- ---- ---- ----
---- ----
'' 0 NaN NaN NaN
false undefined
'8' 8 8 8 8
false 8
'.8' 0.8 NaN NaN 0.8
false 0.8
'-8' -8 -8 -8 -8
false -8
'08' 8 0 8 8
false 8
'1e3' 1000 1 1 1000
false 1000
'1d3' NaN 1 1 1
true ERROR
'0x1d3' 467 467 0 0
false 467
'd' NaN NaN NaN NaN
true ERROR
The numeric operator shortcuts we sometimes use (+n, 1*n, n-0, n/1)
behave exactly the same as Number(n) does.</tip>
--
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>
More information about the thelist
mailing list