Skip to content

Number With Unit

Info

New in Atfinity 17.

A Number (with unit) field holds an amount together with the unit it is measured in, so one salary field can carry 5000, CHF and month.

Reading The Parts

The field behaves like a dictionary holding three keys:

p is Person
p.salary["decimal_number"]      # the amount, 5000
p.salary["unit"]                # the unit, "CHF"
p.salary["item"]                # the "per" part, "month"
p.salary["currency"]            # an unknown key, so nothing

Reading the field itself gives you the amount, so p.salary > 5000 compares the number.

Number With Unit Operations

Arithmetic keeps the unit and the item, taking them from the left value, or from the right one when the left is a plain number:

p.salary * 2                    # 10000 CHF per month

Nothing converts between units. An operation on two values in different units keeps the unit of the left one, and a comparison reads the amounts alone, so p.salary > q.salary is true whenever the left amount is larger, whichever units the two carry.

Three functions work on the type itself:

  • NUMBER_WITH_UNIT(amount, unit, item) builds a value. The unit is required, the item is optional.
  • SAME_UNIT(a, b) is true when both values carry the same unit and the same item.
  • SAME_NUMBER_WITH_UNIT(a, b) is true when they carry the same amount as well.

Assigning One Part

Assign a single key to change one part and leave the other two alone:

p is Person
---
p.salary["unit"] := "CHF"

Assigning the field itself sets all three parts, and assigning a plain number to it keeps the unit and the item the field already has:

p.salary := 5000
  • decimal_number, unit and item are the only keys.
  • Assigning unit or item while the field has no value does nothing, because a unit without a number is not a value. Assign the whole field instead.
  • Assigning decimal_number while the field has no value uses the field's default unit and default unit item.
  • Assigning null to item clears it. decimal_number and unit cannot be cleared, because a value always needs both, so assign null to the whole field instead.
  • A structure member works the same way: p.pets[0].weight["unit"] := "kg".