19.7. Checking Whether the Conversion of a String into a Floating-point Number is Possible: isFloat() Function

The isFloat() function is used to check whether the conversion of a string into a floating-point number is possible. The actual conversion should be done by the parseFloat() function, which will be described in the next section.

The syntax of isFloat() is shown below:


Lang.isFloat(string);


isFloat() returns true if string can be converted into the float type, otherwise it returns false. If there are any errors or if the mobile device does not support floating-point operations, the function returns invalid. (Remember that the float() function is used to check whether a mobile device supports floating-point operations.)

The following WMLScript example demonstrates how to use the isFloat() function:


v = Lang.isFloat(100);
w = Lang.isFloat("100");
x = Lang.isFloat("100.999");
y = Lang.isFloat("WMLScript Tutorial");
z = Lang.isFloat(true);


After executing the above script, the variables v to x have the Boolean value true while y and z have the Boolean value false.

Note that the plus character (+), the minus character (-), the dot character (.) and the letter e can appear in string, like this:


w = Lang.isFloat("+100.999");
x = Lang.isFloat("-100.999");
y = Lang.isFloat("100.999e2");
z = Lang.isFloat("100.999e-2");


After executing the above script, the variables w to z contain the Boolean value true.

If non-numeric characters other than the above four (+ - . e) is present in string, the WMLScript interpreter will see if the characters before the non-numeric character form a valid floating-point value. If yes, the isFloat() function will still return true. Here is an example:


x = Lang.isFloat("100.999abc");
y = Lang.isFloat("100.999e-2abc");


After executing the above script, both x and y have the value true.


19.8. Converting a String to a Floating-point Number: parseFloat() Function

The parseFloat() function is used to convert a string to a floating-point number. Its syntax is:


Lang.parseFloat(string);


If string cannot be converted to the float type, the mobile device does not support floating-point operations or an error occurs, parseFloat() returns an invalid value. (Remember that the float() function is used to check whether a mobile device supports floating-point operations.)

The following WMLScript examples demonstrate the behavior of the parseFloat() function:


x = Lang.parseFloat(100);
y = Lang.parseFloat("100");

After the execution of the script, both x and y have the floating-point value 100.0.


x = Lang.parseFloat("100.999");
y = Lang.parseFloat("+100.999");

After the execution of the script, both x and y have the floating-point value 100.999.


x = Lang.parseFloat("-100.999");

After the execution of the script, x has the floating-point value -100.999.


x = Lang.parseFloat("100.999e2");
y = Lang.parseFloat("100.999e-2");

After the execution of the script, x has the floating-point value 10099.9 and y has the floating-point value 1.00999.


x = Lang.parseFloat("100.999abc");
y = Lang.parseFloat("100.999e-2abc");

After the execution of the script, x has the floating-point value 100.999 and y has the floating-point value 1.00999.


x = Lang.parseFloat("WMLScript Tutorial");
y = Lang.parseFloat(true);

After the execution of the script, both x and y have the invalid value.


Previous Page Page 61 of 71 Next Page


A button for going back to the top of this page