You can convert numeric values in OCL and EAL expressions when the source value does not conform to the type required by an assignment; this page is for modelers working with Integer, Double, Float, and Decimal values.
When to convert a number
Numeric attributes can represent related kinds of values, but they are not always assignment-compatible. In particular, a nullable Double does not conform to a nullable Decimal.
For example, this assignment produces error 998 because the left side is Decimal and the right side is Double:
self.PaymentMenuRequest.VatPercent := vTypAvBiljett.BiljettPrisMomsConvert the source value to the type required by the target:
self.PaymentMenuRequest.VatPercent := vTypAvBiljett.BiljettPrisMoms.todoubleUse an explicit conversion whenever OCL or EAL reports that the assignment arguments do not conform, or when you want the intended target type to be clear in the expression.
Choose the target type
Choose the type that matches how the value will be used. For the behavior and storage settings of each type, see the individual data-type pages.
| Target type | Use it when | Example conversion |
|---|---|---|
| Decimal | You need a precise decimal value, such as money, tax, or a percentage stored with a defined scale. | self.SomeDecimal := self.SomeDouble.todecimal
|
| Double | The receiving attribute or operation requires a Double. | self.SomeDouble := self.SomeDecimal.todouble
|
| Integer | You need a whole-number result. | self.SomeInt := Decimal.create(5.4).ToInt32
|
Float and Double are floating-point types. Do not rely on floating-point equality when small representation differences matter. Use Decimal for values where decimal precision is required, such as monetary values.
Common assignment patterns
The following assignments illustrate numeric conversions. An assignment to a simpler type can lose fractional information or precision.
self.SomeInt := self.SomeDouble
self.SomeDouble := self.SomeInt
self.SomeDecimal := self.SomeInt
self.SomeDecimal := self.SomeDouble.todecimal
self.SomeInt := self.SomeDecimal
self.SomeDouble := self.SomeDecimal.todoubleReview each conversion based on the value you expect to receive. For example, converting a decimal value of 5.4 to an integer removes the fractional part:
Decimal.create(5.4).ToInt32This expression creates the Decimal value 5.4 and converts it to a 32-bit integer. The result is 5; the .4 fraction is lost. See Int32 for the 32-bit integer type.
Conversion operators in OCL and EAL
MDriven makes many operators from C# available in OCL and EAL. Use the conversion operator on the value being converted, then assign the result to a target of the matching type.
| Source value | Target value | Expression |
|---|---|---|
| Double | Decimal | self.SomeDouble.todecimal
|
| Decimal | Double | self.SomeDecimal.todouble
|
| Decimal literal | Int32 | Decimal.create(5.4).ToInt32
|
For additional numeric operators, see Documentation:OCL Number Operators.
Avoid data loss
A value conversion changes the value used by one expression. It does not change the data type of the model attribute.
Before converting:
- Check whether the target type can represent the required fractional value. Converting to an integer can remove the fraction.
- Check whether Decimal precision and scale meet the receiving attribute's requirements. See Documentation:Decimal.
- Treat Double and Float values as floating-point values; their representation can introduce small rounding differences.
- Use the type required by the receiving attribute rather than relying on an implicit conversion.
If you need to change an attribute's stored data type rather than convert a value during an expression, follow Documentation:Attribute or Data Type Conversion. That process can affect persisted data and requires careful handling.
