You can use toReal() in OCL expressions when you need a value represented as a Real, for example before a calculation that requires a fractional result.
Syntax
number.toReal()Return value
toReal() returns a Real with the same numeric value as self.
If self does not represent a real number, the result is invalid.
| Expression | Result |
|---|---|
5.toReal()
|
A Real with value 5
|
2.5.toReal()
|
A Real with value 2.5
|
When to use it
Use toReal() when the expression must have the Real type rather than an integer type. A Real is a numeric value that can contain a fractional part. See Documentation:Double for information about the Real representation.
For example, convert an integer quantity before using it in an expression where you want the result to be treated as a real value:
Order.allInstances->first.Quantity.toReal()If the first order has Quantity equal to 3, the expression returns the Real value 3.
Invalid values
Do not assume that toReal() can convert every value. When the source value does not represent a real number, the operator returns invalid. Account for this when you use the result in derived attributes, constraints, or other OCL expressions.
Related numeric operators
toReal() changes the numeric representation; it does not round or remove a fractional part. To return an integer from a number, use floor() or round() as appropriate.
