fun Int.units(singular: String, plural: String): String
fun Long.units(singular: String, plural: String): String
These extension functions can be used to display a number with its units. The functions take the singular form of the unit (required) and an optional plural form.
The singular form of the unit of measure.
Examples: "inch", "foot", "yard", "meter", "watt", "mile", "hertz"
The plural form of the unit of measure. This is an optional parameter and will default to the singular form with an 's'
added to the end.
Alternative, you may use any of the shorthand notations described below.
Examples: "inches", "feet", "yards", "meters", "watts", "miles", "hertz", "+es", "-ies", "*"
"*"
- Use this shorthand when the plural is the same as the singular, as with "elk" and "moose", for example.
"+suffix"
- Use this shorthand to get the plural form by appending the given suffix to the singular form. For example, "+es"
will add "es"
to the singular form "inch"
to get "inches"
. The default value for plural is "+s"
.
"-suffix"
- Use this shorthand to get the plural form by dropping the last letter of the singular form then appending the given suffix. For example, "-ies"
will first drop the last 'y'
from "royalty"
then append "ies"
to get the plural "royalties"
.
for (i in 1..3) println("""${i.units("inch", "inches")}""")
// 1 inch
// 2 inches
// 3 inches
for (i in 1..3) println("""${i.units("ox", "oxen")}""")
// 1 ox
// 2 oxen
// 3 oxen
for (i in 1..3) println("""${i.units("man", "men")}""")
// 1 man
// 2 men
// 3 men
for (i in 1..3) println("""${i.units("deer", "deer")}""")
// 1 deer
// 2 deer
// 3 deer
for (i in 1..3) println("""${i.units("moose", "moose")}""")
// 1 moose
// 2 moose
// 3 moose
for (i in 1..3) println("""${i.units("parsec")}""")
// 1 parsec
// 2 parsecs
// 3 parsecs
for (i in 1..3) println("""${i.units("meter")}""")
// 1 meter
// 2 meters
// 3 meters
for (i in 1..3) println("""${i.units("inch", "+es")}""")
// 1 inch
// 2 inches
// 3 inches
for (i in 1..3) println("""${i.units("ox", "+en")}""")
// 1 ox
// 2 oxen
// 3 oxen
for (i in 1..3) println("""${i.units("frequency", "-ies")}""")
// 1 frequency
// 2 frequencies
// 3 frequencies
for (i in 1..3) println("""${i.units("berry", "-ies")}""")
// 1 berry
// 2 berries
// 3 berries
for (i in 1..3) println("""${i.units("agency", "-ies")}""")
// 1 agency
// 2 agencies
// 3 agencies
for (i in 1..3) println("""${i.units("deer", "*")}""")
// 1 deer
// 2 deer
// 3 deer
for (i in 1..3) println("""${i.units("moose", "*")}""")
// 1 moose
// 2 moose
// 3 moose
for (i in 1..3) println("""Generate cards with ${i.units("randomly-chosen pick")}""")
// Generate cards with 1 randomly-chosen pick
// Generate cards with 2 randomly-chosen picks
// Generate cards with 3 randomly-chosen picks
for (1 in 1..3) println("""${i.units("randomly-selected card with 5 picks.", "randomly-selected cards with 5 picks each.")}""")
// 1 randomly-selected card with 5 picks.
// 2 randomly-selected cards with 5 picks each.
// 3 randomly-selected cards with 5 picks each.