WMLScript » Lang » Lang.parseFloat

Syntax:
Lang.parseFloat(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. 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

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>
   parseFloat example
   </p>
   <do type="accept">
      <go href="ParseFloatExample.wmls#findparsefloat()" />
   </do>
</card>

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

</wml>
Explanation:

Code for ParseFloatExample.wml

Language(s): WML
Code:
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.go("ParseFloatExample.wml#card2");
   }
   else
   {
      WMLBrowser.setVar("floatnumber", test);
      WMLBrowser.go("ParseFloatExample.wml#card2");
   }
};
Explanation:

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: