I am grateful for the thorough replies.
Originally Posted by Philip M
As its name implies, parseInt() parses a string value and returns an integer. Syntax:- parseInt(string, radix)
- If the string begins with "0x", the radix is 16 (hexadecimal)
- If the string begins with "0", the radix is 8 (octal). This feature is deprecated
- If the string begins with any other value, the radix is 10 (decimal)
parseFloat() returns the real (decimal or floating point) value of the number parsed from a string value. If the first character in the string is a number, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.
You should also take a look at the Number() function.
"We have to remember that horses are only human ...." - Racing commentator
Originally Posted by Dormilich
* - might be important for large integers
Good to know.
Originally Posted by felgall
parseFloat() handles very small numbers and very big numbers
- eg parseFloat('1e100') returns the same number (1 googol - 1 followed by a hundred 0) as
Math.pow(10,100) but without having to calculate what 10 to the 100th power is.
parseInt() handles number bases between 2 and 36 to convert numbers to base 10
- for example parseInt('100',2) returns 4 and parseInt('ff',16) returns 255 and parseInt('z',36) returns 35.
As a side effect both drop any characters from the end that are not numbers
- eg parseInt('39',8) will return 3 and ignore the 9
This side effect is useful when processing style related numbers eg. parseInt('250px',10) returns 250.
If you just want to convert a string to a number without the number being very big/small or in a different number base then Number() is the function to use as it just handles regular numbers without all the extra overheads needed to handle numbers such as '1.35e-25' and '0xffaa'
- for example Number('120') returns 120.