WMLScript » Lang » Lang.parseFloat

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. Your string can also contain exponential designation.

Converts a string into a floating-point number.

The Lang.parseFloat function converts the given string value into a floating-point number.

The function parses the string value until the end is reached or the first invalid character is encountered. If the very first character is not a digit, a plus + sign, or a negative - sign, then an invalid is returned. Also, if an error occurs, invalid is returned. The parsing recognizes the use of exponential designation, such as 94.375e3.

The browser must recognize floating-point numbers. You can test for this by using the Lang.float function.

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

Here are the results of some conversions:

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


<?xml version="1.0"?>

<card id="card1">
   parseFloat example
   <do type="accept">
      <go href="ParseFloatExample.wmls#findparsefloat()" />

<card id="card2">
   float number = $(floatnumber)


Code for ParseFloatExample.wml

Language(s): WML
extern function findparsefloat()
   var result = Dialogs.prompt("Enter number", "");
   var test = Lang.isFloat(result);
   if(test == true)
      var floatnum = Lang.parseFloat(result);
      WMLBrowser.setVar("floatnumber", floatnum);
      WMLBrowser.setVar("floatnumber", test);

In this example, if the string can be converted to floating-point, the converted number is returned. If the string cannot be converted, false is returned.

Language(s): WML

See Also: