Basic HTML version of Foils prepared 23 June 1997

Foil 36 Numeric Parsing Functions

From JavaScript Tutorial for Certificate Course UC Web applications Certificate -- Summer 97. by Geoffrey C. Fox, Tom Scavo


1 There are JavaScript functions (not string methods) that convert strings to numbers
2 parseInt("15") or equivalently parseInt("15",10) both return the number 15 in base 10
3 The optional second argument is the desired radix so that parseInt("15",8) returns 17 in octal
4 If input string begins with "0x" the default radix is 16 (hexadecimal) whereas if it begins with "0" the default radix is 8 (octal) Ñ otherwise, the default radix is 10
5 x = 1 + "1"; // evaluates to "11"
6 x = 1 + parseInt("1"); // evaluates to 2
7 parseFloat(string) returns a floating point value
  • var x = "0.0314E+2"; var y = parseFloat(x); // sets y=3.14
8 On platforms that support it, parseInt and parseFloat return NaN (Not a Number) if parsing fails

in Table To:


Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998