To integer
- Last Updated: March 24, 2022
- 3 minute read
- Corticon
- Version 6.3
- Documentation
SYNTAX
<Decimal>.toInteger
<String>.toInteger
DESCRIPTION
Converts the value in <Decimal> or all characters in <String> to data type Integer. All decimals have fractional portions
truncated during the conversion. Strings are converted ONLY if all characters in <String> are numeric, without a decimal point. If any
non-numeric characters (with the sole exception of a single leading minus sign for
negative numbers) are present in <String>, no
value is returned by the function. Do not use on String values of null or empty String ( '' )
-- a pair of single quote marks -- as that will generate an error message.
USAGE RESTRICTIONS
The Operators row of the table in Summary Table of Vocabulary Usage Restriction applies. No special exceptions.
RULESHEET EXAMPLE
The following Rulesheet uses .toInteger to convert decimal1 and string1 to type Integer and assign them to integer1 and integer2,
respectively.
SAMPLE TEST
Cases when the toInteger operator accepts null and empty values for string attributes
- Prior to evaluating a rule, Corticon checks if any attribute values used in the expressions in the rule are null and, if so, does not execute the rule.
- During expression evaluation, Corticon protects against null pointer exceptions. The expression "test.string.toInteger" will return null if the string is not an integer. However, the expression "test.string.toInteger + 3" will return “3” if the string is not a number – the value 0 being used as the result of the toInteger.
test.integer =test.string.toInteger Here is the
Ruletest output for three tests:- In test 1, the string is empty but not a null value so the expression evaluates and assigns null to integer.
- In test 2, the string is null so the pre-check for null values does not pass and the expression is not evaluated and the value of integer is unchanged
- In test 3, the string is the string “null” but not a null value so the expression evaluates and assigns null to integer. (Note the value “null” here is a string, it could have just as well been “foo”).
test.integer =test.string.toInteger + 3 Here is
the Ruletest output now: - In test 1, the string is empty but not a null value so the expression evaluates. To prevent a NPE during evaluation, the value 0 is used as the result of the toInteger resulting in the expression being “0 + 3” so integer is assigned a value of 3.
- In test 2, the string is null so the pre-check for null values does not pass and the expression is not evaluated and the value of integer is unchanged.
- In test 3, the string is the string “null” but not a null value so the expression evaluates in the same fashion as 1, that is, “0 + 3” and assigns a value of 3.