You use the Decimal data type for attributes and expressions that must retain decimal places accurately, such as amounts, tax rates, quantities, and percentages.
When to use Decimal
Choose Decimal when a fractional value must be stored and calculated with controlled decimal precision. A common example is an invoice amount:
| Attribute | Example value | Why Decimal is appropriate |
|---|---|---|
Amount
|
12345.67
|
The value includes cents and must retain its decimal places. |
VatPercent
|
25.00
|
The rate is a decimal value used in calculations. |
Quantity
|
1.2500
|
The quantity may require a defined number of fractional digits. |
Decimal is the recommended type for money. Unlike Float and Double, Decimal is intended for precise decimal arithmetic rather than binary floating-point approximation.
Configure a Decimal attribute
You control the database representation of a Decimal attribute with its Precision and Scale properties in the property inspector.
- Select the Decimal attribute in MDriven Designer.
- In the property inspector, set Precision to the total number of digits the value can contain.
- Set Scale to the number of digits after the decimal separator.
- Set both properties to values other than
-1. If either Precision or Scale remains-1, neither setting is used.
For example, Precision 38 and Scale 4 creates a number with up to 38 digits in total, of which 4 are decimal digits. A value can therefore have a form such as 12345.6789.
By default on SQL Server, Decimal is stored with 38 digits, including 2 decimal places.
For the underlying meaning and database handling of Precision, Scale, and Length, see Documentation:Precision.
Precision and Scale
| Setting | Meaning | Example |
|---|---|---|
| Precision | The total number of digits in the value. | Precision 10 allows up to 10 digits overall.
|
| Scale | The number of digits after the decimal separator. | Scale 2 reserves two decimal digits, as in 12345.67.
|
With Precision 10 and Scale 2, values such as 12345.67 and 0.12 fit the configured shape.
Use Decimal in OCL and EAL
Decimal values cannot be expressed as text directly in EAL. Create a Decimal value with Decimal.create(...) instead.
Decimal.create(100)
This creates the Decimal value 100. You can also supply other value types to Decimal.create() when you need type casting. See Documentation:Creating numeric types for details.
To convert a string to Decimal, use Decimal.parse():
Decimal.parse('10')
If the text cannot be converted, the result is NULL. For example:
Decimal.parse('x').isNull
This returns true. See Documentation:Parse for parsing behavior.
Convert between numeric types
When an assignment combines different numeric types, explicitly convert the value to the type required by the receiving attribute. For example, convert a Double value to Decimal with toDecimal:
self.SomeDecimal := self.SomeDouble.toDecimal
The toDecimal operator converts Integer or Float values to Decimal and can round very long decimal values to fit Decimal limits. See toDecimal.
Conversions to simpler numeric types can lose fractional information. For example, converting 5.4 to a 32-bit integer discards the .4 fraction:
Decimal.create(5.4).ToInt32
For assignment rules and further conversion examples, see Documentation:Number conversions.
Calculation guidance
Use Decimal consistently through a calculation that must preserve decimal accuracy. For example, when an amount and a tax percentage are Decimal attributes, keep the result as Decimal rather than converting intermediate values to Float or Double.
Use the normal division operator when you need a Decimal or Real result. Do not use div when the fractional remainder matters: div returns only the integer part of a quotient. For example, 5.div(2) discards the fractional remainder. See Documentation:OCLOperators div.
See also
- Documentation:Precision
- Documentation:Creating numeric types
- toDecimal
- Documentation:Number conversions
- Documentation:Parse
- Documentation:Float
[[Category:Attributes and data types [Beginner]]
