Table of Contents

Benefits Setup Usage

Import these wherever you want to use units-of-measure:

import info.kunalsheth.units.generated.*
import info.kunalsheth.units.math.*

The basic syntax to create a quantity is MAGNITUDE.UNIT.

846.Ounce
1678.Mile
9.71.Minute

Metric prefix functions are also provided. MAGNITUDE.PREFIX(UNIT).

1323.kilo(Gram)
148.milli(Metre)
2.54.deca(Metre)

Math operations work as expected.

val sum =        mass1 + mass2
val myLength = 1.Metre - 3.Foot
val distance =    rate * time
val mySpeed =  65.Mile / Hour

You can also create ranges using the .. operator or the plusOrMinus infix function.

65.Mile / Hour * 27.Minute in 30.Mile..29.Mile       == true
2.Foot / 1.Metre in 60.Percent plusOrMinus 5.Percent == true

inline infix fun <Q : Quan<Q>> Q.`±`(radius: Q) = this plusOrMinus radius
420.Degree % 1.Turn in 60.Degree `±` 1.Degree        == true

== also works, but it is not recommended since floating point math is not exact.

(1.kilo(Gram) == 1000.Gram)         == true
(10.milli(Metre) == 1.centi(Metre)) == true
(60000.milli(Second) == 1.Minute)   == true

Note: quantities are represented by types. Do not use units as types.

fun bad(x: FootPerSecond) { ... } // this is incorrect.
bad(3.FootPerSecond)              // this won't compile

data class Car(val topSpeed: Speed, val floorIt: Acceleration) { ... }  // good
val kunalsCar = Car(217.Mile / Hour, 100.Mile / Hour / 4.7.Second)      // good

fun v(a: Acceleration, v0: Velocity, t: Time): Velocity = v0 + a * t    // good

‹ Configure Advanced Usage ›