# F# Conversions between units

## Example

``````[<Measure>] type m // meters
[<Measure>] type cm // centimeters

// Conversion factor
let cmInM = 100<cm/m>

let distanceInM = 1<m>
let distanceInCM = distanceInM * cmInM // 100<cm>

// Conversion function
let cmToM (x : int<cm>) = x / 100<cm/m>
let mToCm (x : int<m>) = x * 100<cm/m>

cmToM 100<cm> // 1<m>
mToCm 1<m> // 100<cm>
``````

Note that the F# compiler does not know that `1<m>` equals `100<cm>`. As far as it cares, the units are separate types. You can write similar functions to convert from meters to kilograms, and the compiler would not care.

``````[<Measure>] type kg

// Valid code, invalid physics
let kgToM x = x / 100<kg/m>
``````

It is not possible to define units of measure as multiples of other units such as

``````// Invalid code
[<Measure>] type m = 100<cm>
``````

However, to define units "per something", for example Hertz, measuring frequency, is simply "per second", is quite simple.

``````// Valid code
[<Measure>] type s
[<Measure>] type Hz = /s

1 / 1<s> = 1 <Hz> // Evaluates to true

[<Measure>] type N = kg m/s // Newtons, measuring force. Note lack of multiplication sign.

// Usage
let mass = 1<kg>
let distance = 1<m>
let time = 1<s>

let force = mass * distance / time // Evaluates to 1<kg m/s>
force = 1<N> // Evaluates to true
`````` PDF - Download F# for free