F# units of measure
last modified December 3, 2026
In this article we attach units of measure to numbers in F#.
A number in a program often stands for a quantity: a distance in metres, a time in seconds or a price in euros. Units of measure let the compiler track these units. Adding metres to seconds becomes a compile error, and dividing metres by seconds gives metres per second. The units exist only at compile time; the program runs exactly as fast as with plain numbers.
Defining units
A unit is declared as a type with the [<Measure>]
attribute. A number gets a unit when the unit is written in angle brackets
after it.
[<Measure>] type m [<Measure>] type s let distance = 100.0<m> let time = 9.58<s> let speed = distance / time printfn "%.2f m/s" speed
We compute the average speed of a 100 metre sprint.
let distance = 100.0<m>
distance has the type float<m>. It is still a
float, but the compiler knows that it is measured in metres.
let speed = distance / time
Dividing metres by seconds gives a value of type
float<m/s>. The units are not part of the printed text;
printfn prints the plain number.
λ dotnet fsi units.fsx 10.44 m/s
Types with units
F# Interactive shows the type of each value, so it is a good place to see how units combine. Multiplying adds exponents and dividing subtracts them.
let ratio = 6.0<m> / 2.0<m>;;
Metres times metres are square metres, written m ^ 2. When
the units cancel, as in ratio, the result is a plain
float. The compiler simplifies the units itself:
m/s divided by s is m/s ^ 2.
λ dotnet fsi > [<Measure>] type m;; [<Measure>] type m > [<Measure>] type s;; [<Measure>] type s > let side = 4.0<m>;; val side: float<m> = 4.0 > let area = side * side;; val area: float<m ^ 2> = 16.0 > let volume = area * 2.5<m>;; val volume: float<m ^ 3> = 40.0 > let speed = 30.0<m> / 4.0<s>;; val speed: float<m/s> = 7.5 > let accel = speed / 2.0<s>;; val accel: float<m/s ^ 2> = 3.75 > let ratio = 6.0<m> / 2.0<m>;; val ratio: float = 3.0
Mismatched units
Values can only be added, subtracted and compared when they have the same unit. A mistake is reported before the program runs.
[<Measure>] type m [<Measure>] type s let width = 3.5<m> let duration = 12.0<s> let total = width + duration
The script adds a length to a time.
let total = width + duration
The compiler reports error FS0001: it expected metres and got seconds. The same check catches subtler errors, for example a formula that divides where it should multiply.
λ dotnet fsi mismatch.fsx mismatch.fsx(7,21): error FS0001: The unit of measure 's' does not match the unit of measure 'm'
Derived units
A unit can be defined in terms of other units. The definition is written as a unit formula after the equals sign; a space between units means multiplication.
[<Measure>] type kg [<Measure>] type m [<Measure>] type s [<Measure>] type N = kg m / s^2 [<Measure>] type J = N m [<Measure>] type W = J / s let mass = 1200.0<kg> let acceleration = 2.5<m/s^2> let force: float<N> = mass * acceleration let work: float<J> = force * 40.0<m> let power: float<W> = work / 8.0<s> printfn "force: %.0f N" force printfn "work: %.0f J" work printfn "power: %.0f W" power
Newtons, joules and watts are defined from kilograms, metres and seconds.
let force: float<N> = mass * acceleration
The product has the unit kg m/s^2, which is the definition of
N, so the annotation is accepted. The compiler treats a
derived unit and its formula as the same unit. An annotation with
float<J> here would be rejected.
λ dotnet fsi derived.fsx force: 3000 N work: 120000 J power: 15000 W
Converting between units
A conversion is a multiplication by a factor that has a unit itself, such as metres per kilometre. The units of the factor cancel the old unit and bring in the new one.
[<Measure>] type m [<Measure>] type km [<Measure>] type cm [<Measure>] type h [<Measure>] type s let metresPerKm = 1000.0<m/km> let cmPerMetre = 100.0<cm/m> let secondsPerHour = 3600.0<s/h> let toMetres (d: float<km>) = d * metresPerKm let toCm (d: float<m>) = d * cmPerMetre let trip = 12.4<km> printfn "%.1f km = %.0f m = %.0f cm" trip (toMetres trip) (toCm (toMetres trip)) let limit = 90.0<km/h> let limitMs = limit * metresPerKm / secondsPerHour printfn "%.0f km/h = %.1f m/s" limit limitMs
Kilometres are converted to metres and centimetres, and a speed limit to metres per second.
let limitMs = limit * metresPerKm / secondsPerHour
The units work out as km/h * m/km / (s/h), which simplifies to
m/s. If the factor were used the wrong way round, the result
would have the wrong unit, and the compiler would reject it wherever
m/s is expected.
λ dotnet fsi conversion.fsx 12.4 km = 12400 m = 1240000 cm 90 km/h = 25.0 m/s
Removing and adding units
.NET methods such as Math.Round or Double.Parse
know nothing about units. A unit is removed by dividing by
1.0 of the unit or with the float function, and
added by multiplying by 1.0 of the unit.
[<Measure>] type kg let parseWeight (text: string) = float text * 1.0<kg> let roundWeight (w: float<kg>) = System.Math.Round(w / 1.0<kg>, 1) * 1.0<kg> let w = parseWeight "72.46" printfn "%.2f kg" w let plain: float = float w printfn "%.2f" plain let rounded = roundWeight w printfn "%.1f kg" rounded let fromLibrary = LanguagePrimitives.FloatWithMeasure<kg> 68.0 printfn "difference: %.1f kg" (rounded - fromLibrary)
The weight is read from text, rounded with a .NET method and compared with another weight.
let roundWeight (w: float<kg>) = System.Math.Round(w / 1.0<kg>, 1) * 1.0<kg>
Dividing by 1.0<kg> gives a plain float for
Math.Round, and the result is multiplied by
1.0<kg> to get the unit back. Keeping the conversions at
the edges of a program leaves the rest of the code unit-safe.
let fromLibrary = LanguagePrimitives.FloatWithMeasure<kg> 68.0
FloatWithMeasure adds a unit to a float that is already
computed. It does the same as the multiplication, but it says what is
meant.
λ dotnet fsi strip.fsx 72.46 kg 72.46 72.5 kg difference: 4.5 kg
Generic units
A function can work with any unit. The unit parameter is written like a
type parameter, for example float<'u>, and the result
keeps the unit of the arguments.
[<Measure>] type m [<Measure>] type s [<Measure>] type kg let average (xs: float<'u> list) = List.sum xs / float xs.Length let square (x: float<'u>) = x * x let spread (xs: float<'u> list) = List.max xs - List.min xs let heights = [1.72<m>; 1.85<m>; 1.64<m>; 1.79<m>] let laps = [61.2<s>; 59.8<s>; 60.5<s>] let weights = [72.0<kg>; 81.5<kg>; 64.0<kg>] printfn "height: %.2f m, spread %.2f m" (average heights) (spread heights) printfn "lap: %.2f s, spread %.2f s" (average laps) (spread laps) printfn "weight: %.1f kg" (average weights) let floor: float<m^2> = square 3.5<m> printfn "floor: %.2f m^2" floor
The same three functions work with metres, seconds and kilograms.
let average (xs: float<'u> list) = List.sum xs / float xs.Length
List.sum keeps the unit of the elements, and dividing by a
plain float leaves it unchanged. The result of average heights
is a float<m>.
let square (x: float<'u>) = x * x
The inferred type of square is
float<'u> -> float<'u ^ 2>, so squaring metres gives
square metres.
λ dotnet fsi generic_units.fsx height: 1.75 m, spread 0.21 m lap: 60.50 s, spread 1.40 s weight: 72.5 kg floor: 12.25 m^2
Units in records
Record fields and union cases can carry units. The types then document what each number means.
[<Measure>] type kg
[<Measure>] type m
[<Measure>] type s
type Planet = { Name: string; Mass: float<kg>; Radius: float<m> }
let G = 6.674e-11<m^3/(kg s^2)>
let gravity planet = G * planet.Mass / (planet.Radius * planet.Radius)
let weightOn planet (mass: float<kg>) = mass * gravity planet
let planets =
[ { Name = "Mercury"; Mass = 3.301e23<kg>; Radius = 2.4397e6<m> }
{ Name = "Earth"; Mass = 5.972e24<kg>; Radius = 6.371e6<m> }
{ Name = "Mars"; Mass = 6.417e23<kg>; Radius = 3.3895e6<m> }
{ Name = "Jupiter"; Mass = 1.898e27<kg>; Radius = 6.9911e7<m> } ]
for p in planets do
printfn "%-8s g = %5.2f m/s^2, 80 kg weighs %6.1f N" p.Name (gravity p) (weightOn p 80.0<kg>)
The surface gravity of a planet follows from its mass and radius.
let G = 6.674e-11<m^3/(kg s^2)>
The gravitational constant has a compound unit. With it,
gravity returns m/s^2, and the weight, a mass
times an acceleration, has the unit kg m/s^2, which is a
newton.
λ dotnet fsi planets.fsx Mercury g = 3.70 m/s^2, 80 kg weighs 296.1 N Earth g = 9.82 m/s^2, 80 kg weighs 785.6 N Mars g = 3.73 m/s^2, 80 kg weighs 298.2 N Jupiter g = 25.92 m/s^2, 80 kg weighs 2073.4 N
Integer and decimal units
Units are not limited to floats. Integers and decimals can carry them too, which suits counts and money.
[<Measure>] type EUR
[<Measure>] type pcs
type Line = { Item: string; Price: decimal<EUR/pcs>; Quantity: int<pcs> }
let lineTotal line =
line.Price * LanguagePrimitives.DecimalWithMeasure<pcs> (decimal line.Quantity)
let lines =
[ { Item = "notebook"; Price = 3.40m<EUR/pcs>; Quantity = 12<pcs> }
{ Item = "pen"; Price = 0.85m<EUR/pcs>; Quantity = 30<pcs> }
{ Item = "stapler"; Price = 11.90m<EUR/pcs>; Quantity = 2<pcs> } ]
for l in lines do
printfn "%-9s %3d x %5.2f = %6.2f" l.Item l.Quantity l.Price (lineTotal l)
let items = lines |> List.sumBy (fun l -> l.Quantity)
let total = lines |> List.sumBy lineTotal
printfn "%d pieces, total %.2f EUR" items total
An invoice multiplies prices in euros per piece by quantities in pieces.
line.Price * LanguagePrimitives.DecimalWithMeasure<pcs> (decimal line.Quantity)
A decimal cannot be multiplied by an integer, so the quantity is converted.
The decimal function drops the unit, and
DecimalWithMeasure puts it back. The result has the unit
EUR/pcs * pcs, which is EUR.
λ dotnet fsi invoice.fsx notebook 12 x 3.40 = 40.80 pen 30 x 0.85 = 25.50 stapler 2 x 11.90 = 23.80 44 pieces, total 90.10 EUR
Running log
The last example keeps a log of runs with distances in kilometres and times in minutes, and computes the pace and speed of each run.
[<Measure>] type km
[<Measure>] type min
[<Measure>] type h
let minutesPerHour = 60.0<min/h>
type Run = { Date: string; Distance: float<km>; Time: float<min> }
let pace run = run.Time / run.Distance
let speed run = run.Distance / run.Time * minutesPerHour
let formatPace (p: float<min/km>) =
let total = int (round (float p * 60.0))
$"{total / 60}:{total % 60:D2}"
let runs =
[ { Date = "2026-11-02"; Distance = 5.0<km>; Time = 27.5<min> }
{ Date = "2026-11-09"; Distance = 8.2<km>; Time = 46.1<min> }
{ Date = "2026-11-16"; Distance = 10.0<km>; Time = 54.3<min> }
{ Date = "2026-11-23"; Distance = 6.5<km>; Time = 33.8<min> } ]
printfn "date distance pace/km speed"
for r in runs do
printfn "%s %5.1f km %7s %4.1f km/h" r.Date r.Distance (formatPace (pace r)) (speed r)
let totalDistance = runs |> List.sumBy (fun r -> r.Distance)
let totalTime = runs |> List.sumBy (fun r -> r.Time)
let best = runs |> List.minBy pace
printfn "total %.1f km in %.1f h" totalDistance (totalTime / minutesPerHour)
printfn "best pace %s/km on %s" (formatPace (pace best)) best.Date
The pace is minutes per kilometre and the speed is kilometres per hour.
let pace run = run.Time / run.Distance let speed run = run.Distance / run.Time * minutesPerHour
The types are inferred as float<min/km> and
float<km/h>. Swapping the division in pace
would give km/min, and the call to formatPace
would no longer compile.
let total = int (round (float p * 60.0))
To print the pace as minutes and seconds, the unit is dropped with
float and the value is turned into whole seconds.
List.sumBy and List.minBy keep the units of the
values they work with.
λ dotnet fsi running.fsx date distance pace/km speed 2026-11-02 5.0 km 5:30 10.9 km/h 2026-11-09 8.2 km 5:37 10.7 km/h 2026-11-16 10.0 km 5:26 11.0 km/h 2026-11-23 6.5 km 5:12 11.5 km/h total 29.7 km in 2.7 h best pace 5:12/km on 2026-11-23
Source
F# language reference: Units of measure
In this article we have defined units with the [<Measure>]
attribute, combined and derived units, converted between them, removed and
added units at the edges of a program, written functions generic over
units, and used units in records, integers and decimals. Records are
covered in F# records, numeric types in
F# data types, and generic type parameters
in F# generics.
Author
List all F# tutorials.