Unit Formats
Unit Formats control the display and formatting of units for numeric values, such as time intervals, length or distance, mass or weight.
Understanding Unit Formats
Without a unit format, the numeric value displays the full name of the unit type. For example:
put 1 mm --> 1 millimeter
put 3 kg --> 3 kilograms
In many cases, the default behavior is perfectly fine, but there are other times when you need more control of formatting. For example:
put 4425 mins --> 4425 minutes
put 4425 mins as hrs --> 73.75 hours
put 4425 mins as days --> 3.072917 days
Without using a unit format, you can only display 4425 minutes as minutes, or as hours, or as days. A better format might be 3 days 1 hour 45 minutes, which is much easier to understand. To achieve this, you can apply either of the following formats:
"[day] days [hr] hours [min] minutes"
"[days] @ [hours] @ [minutes] @"
Elements of Unit Formats
Unit formats can range from simple to rather complex. The easiest way to understand them is to begin with the basics and add in other options as required.
The Basics
The most basic unit formats are text strings where one or more unit tokens, or unit names, are replaced by a number in the final result. You can use a singular or plural unit name, or even an abbreviation, and capitalization doesn't matter. The name just needs to identify a unit type. For example:
put 5 kg -->5 kilograms
put 5 kg with format "[KG] kg" --> 5 kg
put 5 kg with format "[Kilogram] kg" --> 5 kg
put 5 kg with format "[kilograms] kg" --> 5 kg
The "@" token requires more precise syntax. For full details, see Unit Names (@ tokens).
If more than one unit name is included in the unit format, SenseTalk works from left to right, replacing each token in turn with the whole number of units of that type that add up to the numeric value. In the following example, SenseTalk:
- Determines that 483 seconds is equal to 8.05 minutes.
- Replaces the
[min]token with the whole number 8. - Allocates the remainder, 0.05 minutes or 3 seconds, to the next token.
put 483 secs format "[min] minutes [s] seconds" --> 8 minutes 3 seconds
The allocation of values from left to right means that the unit formatting system is only able to produce formatted values beginning with larger units followed by progressively smaller units.
Unit Names in Tokens
As mentioned earlier, the syntax that you use for unit tokens, or unit names, is flexible. You can include singular, or plural names, or even abbreviations. For example, you can write the unit token that represents hours as [hours] or [hour] or [hrs] or [hr]. The following unit format strings look slightly differently but apply the same formatting.
"[hr]:[min]:[sec]"
"[hours]:[minutes]:[seconds]"
...and result in
3 seconds --> 0:0:3
521957 seconds --> 144:59:17
Number Formats
Number formats let you control whether whole numbers or fractions, including the number of digits and decimal places, are displayed. In the following example, the dot in [seconds .] allows the display of any fractions of a second:
"[hours]:[minutes]:[seconds .]"
...results in
63.6487 seconds --> 0:1:3.6487
You can only use the dot in the final unit token of the format. This is the remainder of the value after the rest has been allocated to the larger unit tokens, or types. The "." is equivalent to using a number format of 0.######.
The .### in [seconds .###] displays fractions of a second up to three decimal places:
"[hours]:[minutes]:[seconds .###]"
...results in
63.6487 seconds --> 0:1:3.649
The 00 in [minutes 00] displays two digits for this unit type, even if there are zero minutes. The 00.000 in [seconds 00] displays two digits before, and three digits after the decimal place, to display fractions of a second or zeros:
"[hours]:[minutes 00]:[seconds 00.000]"
...results in
63.6487 seconds —> 0:01:03.649
2.4 seconds —> 0:00:02.400