You use toDecimal in OCL expressions when you need an Integer or Float value to be treated as a Decimal, especially before using it with Decimal attributes.
Purpose
toDecimal converts a numeric value to a Decimal value.
Use it to align the numeric types on both sides of a calculation. For example, when Total is a Decimal attribute, convert the numeric constant before multiplying it:
self.Total * 0.10.ToDecimalIf self.Total is 50.00, this expression evaluates to 5.0.
Syntax
Call toDecimal on the numeric value that you want to convert:
number.toDecimalThe operator converts an Integer or Float to Decimal.
| Input value type | Expression | Result type |
|---|---|---|
| Integer | 45.toDecimal
|
Decimal |
| Float | 0.10.ToDecimal
|
Decimal |
Use Decimal values in calculations
Use toDecimal when a calculation combines a Decimal attribute with a numeric literal or another value that is not already Decimal.
For example, this mixed-type formula can produce a type error when Total is Decimal:
self.Total * 0.10Convert the constant to Decimal to align the operands:
self.Total * 0.10.ToDecimalThis is particularly relevant when an expression is evaluated dynamically with ScriptEval. The ScriptEval example uses this conversion to correct an error stating that no multiplication operator accepts the mixed parameter types.
Conversion behavior
- Use
toDecimalwith numbers, not with text. - The conversion can round very long decimal values to fit Decimal limits.
- Convert values at the point where their type must match the rest of the expression. In the example above, converting
0.10makes it compatible with a DecimalTotal.
For conversion of text values, see Parse. Date and DateTime text conversions are documented separately in strToDate and strToDateTime.
