19.5. Checking Whether the Conversion of a String into an Integer is Possible: isInt() Function
The isInt() function is used to check whether the conversion of a string into an integer is possible. The actual conversion should be done by the parseInt() function, which will be described in the next section.
The syntax of isInt() is:
Lang.isInt(string);
isInt() returns true if string can be converted into the integer type, otherwise it returns false. If there are any errors, the function returns invalid. The + (optional) and - character are used to represent positive and negative values respectively.
The following WMLScript example demonstrates the behavior of the isInt() function:
u
= Lang.isInt("100");
v = Lang.isInt(" 100 ");
w
= Lang.isInt("+100");
x = Lang.isInt("-100");
y
= Lang.isInt("WMLScript Tutorial");
z =
Lang.isInt(true);
After executing the above script, the variables u to x have the Boolean value true while y and z have the Boolean value false.
Note that if the WMLScript interpreter encounters a non-numeric character in string, it will check if the characters before the non-numeric character form a valid integral value. If yes, the isInt() function will still return true, like this:
x
= Lang.isInt("100abc");
y = Lang.isInt("100.999");
z
= Lang.isInt(100.8);
After the execution of the above lines of code, the variables x to z contain the Boolean value true.
19.6. Converting a String to an Integer: parseInt() Function
The parseInt() function is used to convert a string to an integer. Its syntax is:
Lang.parseInt(string);
If string cannot be converted to the integer type or if there are any errors, parseInt() returns an invalid value.
The following WMLScript examples demonstrate the behavior of the parseInt() function:
x
= Lang.parseInt("100");
y =
Lang.parseInt(" 100 ");
z = Lang.parseInt("+100");
After executing the above script, the variables x to z have the integral value 100.
z = Lang.parseInt("-100");
After executing the above script, z has the integral value -100.
x
= Lang.parseInt("WMLScript Tutorial");
y =
Lang.parseInt(true);
After executing the above script, both x and y have the invalid value.
x
= Lang.parseInt("100abc");
y =
Lang.parseInt("100.999");
z = Lang.parseInt(100.8);
After executing the above script, the variables x to z have the integral value 100.
Previous Page | Page 60 of 71 | Next Page | ![]() |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data