🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators Number::abs () : Number
This page was created by Peter on 2019-11-25. Last edited by Wikiadmin on 2026-07-29.

You can use abs() in OCL expressions when you need the non-negative magnitude of an Integer or Real value.

Signature

Number::abs() : Number

Result

Returns the absolute value of self.

  • A negative number is returned without its minus sign.
  • Zero is returned as zero.
  • A positive number is returned unchanged.
(-12).abs()  -- 12
0.abs()      -- 0
4.5.abs()    -- 4.5

When to use abs()

Use abs() when the direction of a difference does not matter and you need its size.

For example, the following expression returns the distance between two numeric values as a non-negative number:

(self.ActualValue - self.ExpectedValue).abs()

If ActualValue is 92 and ExpectedValue is 100, the result is 8. If the values are reversed, the result is also 8.

Notes

In OCL number operators, Number covers both Integer and Real values unless an operation specifies otherwise. abs() returns a Number; it does not round or otherwise convert the value.

Use floor() or round() when you need an integer result. Use min() or max() to select the lower or greater of two values.

See also