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:


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:


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

Feedback Form (ExpandCollapse)

What do you think about this web page?

(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.