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:
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.
A list aggregate keeps the unit as well.
MIN and MAX return one of the values, so they carry that value's unit, while SUM and AVG take the unit and the
item from the first value in the list that carries one:
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:
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:
decimal_number,unitanditemare the only keys.- Assigning
unitoritemwhile the field has no value does nothing, because a unit without a number is not a value. Assign the whole field instead. - Assigning
decimal_numberwhile the field has no value uses the field's default unit and default unit item. - Assigning
nulltoitemclears it.decimal_numberandunitcannot be cleared, because a value always needs both, so assignnullto the whole field instead. - A structure member works the same way:
p.pets[0].weight["unit"] := "kg". - The key can be a variable holding one of the three names (new in Atfinity 17).