WMLScript » Lang » Lang.parseInt

Syntax:
Lang.parseInt(value)
value
The mandatory value parameter can be any string that begins with numeric characters or begins with a plus or minus sign followed by numeric characters.

Converts a string into an integer.

The Lang.parseInt function converts the given string value into an integer number.

The function parses the string value until the end is reached or the first invalid character is encountered. Parsing does not continue past a decimal point or an e exponential designator. If the very first character is not a digit, a plus + sign, or a negative - sign, invalid is returned. Also, if an error occurs, invalid is returned.

You can also use the Lang.isInt function to test a string to pre-determine if it can be converted into an integer number. This can prevent invalid from being returned.

The Float.int function will return the integer portion of a floating-point number.

Here are the results of some conversions:

Value Return
"101" 101
"-101" -101
"984.653" 984
"42 feet" 42
"7 inches by 8 inches" 7
"75e3" 75
"83.67e-9" 83
"$12.99" invalid
"The size is 4x8 feet" invalid

Examples

Code:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.WAPforum.org/DTD/wml_1.1.xml">
<wml>

<card id="card1">
   <p>
   parseInt example
   </p>
   <do type="accept">
      <go href="ParseIntExample.wmls#findparseint()" />
   </do>
</card>

<card id="card2">
<p>
   integer number = $(intnumber)
</p>
</card>

</wml>
Explanation:

Code for ParseIntExample.wml

Language(s): WML
Code:
extern function findparseint()
{
   var result = Dialogs.prompt("Enter number", "");
   var test = Lang.isInt(result);
   if(test == true)
   {
      var intnum = Lang.parseInt(result);
      WMLBrowser.setVar("intnumber", intnum);
      WMLBrowser.go("ParseIntExample.wml#card2");
   }
   else
   {
      WMLBrowser.setVar("intnumber", test);
      WMLBrowser.go("ParseIntExample.wml#card2");
   }
};
Explanation:

In this example, if the string can be converted to an integer, the converted number is returned. If the string cannot be converted, false is returned.

Language(s): WML

See Also: