Lang.parseInt
Lang.float
Lang.isFloat
Lang.isInt
Float.int
FUNCTION: Lang.parseFloat
Lang.parseFloat
(value)
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.
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.
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
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.
Code for ParseFloatExample.wml
<?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>
Code for ParseFloatExample.wmls
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");
}
};
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information