Converting between types

Projects often have a mix of different types, and it is common to require conversion between different types. Which conversion is required depends on which types are involved.

Converting from text to another type

Converting from text is primarily done using the from text(..) and from text to(..) functions. The destination type that is being converted from needs to be known ahead of time. Often this is apparent from the context and thus the simpler from text(..) can be used. In cases where this is not possible, you will be prompted to use from text to(..) instead, where the type is specified.

Examples
from text("63.3") + 164.3
@if from text("true") @then 1 @else 0 @endif1
from text("true") + 1error
from text to(type{Time}, "7:35PM")time{19:35}
from text to(type{[Number]}, "[1, 2]")[1, 2]
Converting from another type to text

Converting to text is primarily done using the to text(..) function. This converts each type into its standard text form. Several examples are below. Most types are straightforward, although note that times convert to the 24-hour clock and dates convert to the ISO 8601 YYYY-MM-DD format.

Examples
to text(10 - 45.60)"-35.6"
to text([true, "a" = "b"])"[true, false]"
to text(datetime{14 April 2008 5:38PM})"2018-04-14 17:38"
Converting numbers between different units

There are three main types of conversion (with more examples below):

Examples
convert unit({mile/hour}, 10{m/s})22.369 ± 0.001
convert unit({m/s}, 1{mile} / 4{minute})6.705 ± 0.001
5262.3 * 1{kg}5262.3{kg}
32.5{year} / 1{year}32.5
Converting between different date and time types

Converting between different date and time types is usually a matter of composing, decomposing or differencing.

Composing means making smaller parts into larger, for example converting a Date and a Time into a DateTime, or converting a year and a month into a DateYM. Such functions are:

Decomposing larger parts into smaller items, for example extracting the Date from a DateTime, or the year from a Date. Such functions are:

Differencing means comparing two items of the same date or time type, to work out the distance between them. Such functions are:

Examples
dateym from ym(2008{year}, 3{month})dateym{2008-03}