🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
ToInteger () : Integer
This page was created by Peter on 2019-11-22. Last edited by Wikiadmin on 2026-07-29.

You can use toInteger() in an OCL expression when you need to convert a value that represents a whole number into an Integer.

Syntax

self.toInteger()

Return value

toInteger() returns an Integer whose value equals self when self represents an integer.

If self does not represent an integer, the operation returns |invalid|.

Expression Result
'42'.toInteger() The Integer value 42.
'-7'.toInteger() The Integer value -7.
'42.5'.toInteger() invalid|, because the value does not represent an integer.
'abc'.toInteger() invalid|, because the value does not represent an integer.

When to use it

Use toInteger() when the source value is expected to be an integer representation and you need an Integer for subsequent calculations or comparisons.

For example, use the converted value in an integer division:

'10'.toInteger().div(3)

div returns the integer quotient. See Documentation:OCLOperators Integer::div ( i : Integer ) : Integer.

Conversion is not rounding

toInteger() does not convert a non-integer representation such as '42.5' by removing or rounding its fractional part; it returns |invalid| instead.

When you already have a Number and need an integer result based on numeric value, use the operation that matches the required rule:

Need Use
The integer part of a Real Number::floor()
The nearest integer to a Real Number::round()
Convert a value that represents an integer toInteger()

Invalid values

Check or handle values that may not represent integers before relying on the result. A conversion that returns |invalid| cannot be used as a valid Integer value in later expression logic.

See also

[[Category:Attributes and data types [Beginner]]]