No edit summary |
No edit summary |
||
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
The numeric types float, double, decimal and int are sort of apples | <message>Write the content here to display this box</message> | ||
The numeric types [[Documentation:Float|float]], [[Documentation:Double|double]], [[Documentation:Decimal|decimal]], and [[Documentation:Integer|int]] are sort of apples from the same tree and MDriven exposes ways to go from all numeric types to decimal. The operator is called '''toDecimal'''. | |||
Sometimes you may want to assign from one type to another like this: | Sometimes, you may want to assign from one type to another like this: | ||
self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms | self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms | ||
but you get an error like | but you get an error like 998: In <code>'''<span class="col-black">":="</span>'''</code>, one of the arguments must conform to the other (<code><span class="col-black">'''Nullable<System.Double>'''</span></code> and <code><span class="col-black">'''Nullable<System.Decimal>'''</span></code> do not). | ||
Solve like this: | Solve like this: | ||
self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms.todouble | self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms.todouble | ||
These are valid assignments but you change precision and | These are valid assignments, but you change precision and lose fractions when converting to simpler types: | ||
self.SomeInt:=self.SomeDouble | self.SomeInt:=self.SomeDouble | ||
self.SomeDouble:=self.SomeInt | self.SomeDouble:=self.SomeInt | ||
self.SomeDecimal:=self.SomeInt | self.SomeDecimal:=self.SomeInt | ||
self.SomeDecimal:=self.SomeDouble.todecimal | self.SomeDecimal:=self.SomeDouble.todecimal | ||
self.SomeInt:=self.SomeDecimal | self.SomeInt:=self.SomeDecimal | ||
self.SomeDouble:=self.SomeDecimal.todouble | |||
In OCL, you can also use a lot of the operators from C#, which are automatically added to the OCL and EAL language. | |||
For example, | |||
Decimal.create(5.4).ToInt32 | |||
Creates a decimal with the value 5.4 and then converts it to an 32 bit integer, losing the fraction .4 in the process. | |||
[[Category:OCL]] | [[Category:OCL]] | ||
{{Edited|July|12|2025}} |
Latest revision as of 05:03, 22 January 2025
The numeric types float, double, decimal, and int are sort of apples from the same tree and MDriven exposes ways to go from all numeric types to decimal. The operator is called toDecimal.
Sometimes, you may want to assign from one type to another like this:
self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms
but you get an error like 998: In ":="
, one of the arguments must conform to the other (Nullable<System.Double>
and Nullable<System.Decimal>
do not).
Solve like this:
self.PaymentMenuRequest.VatPercent:=vTypAvBiljett.BiljettPrisMoms.todouble
These are valid assignments, but you change precision and lose fractions when converting to simpler types:
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.todouble
In OCL, you can also use a lot of the operators from C#, which are automatically added to the OCL and EAL language.
For example,
Decimal.create(5.4).ToInt32
Creates a decimal with the value 5.4 and then converts it to an 32 bit integer, losing the fraction .4 in the process.