Scala library to aid with conversion of units of measurement.
"io.flow" %% "lib-unit-of-measurement" % "0.0.96"
import io.flow.units.v0.models.{UnitOfLength, UnitOfWeight}
import io.flow.uom.v2.{Length, Weight}
val pounds = Weight(1, UnitOfWeight.Kilogram).convertTo(UnitOfWeight.Pound)
println(s"1 kilogram is equivalent to ${pounds.value} pounds")
val feet = Length(12, UnitOfLength.Inch).convertTo(UnitOfLength.Foot)
println(s"12 inches is equivalent to ${feet.value} foot")
import io.flow.common.v0.models.UnitOfMeasurement
import io.flow.uom.Converter
val converter = Converter()
converter.fromString("lbs") // => Some(UnitOfMeasurement.Pound)
converter.convert(2, UnitOfMeasurement.Kilogram, UnitOfMeasurement.Gram) match {
case Left(errors) => println(error)
case Right(grams) => println(s"2 KG = $grams grams")
}
converter.toGrams(2, UnitOfMeasurement.Gram) match {
case Left(errors) => println(error)
case Right(grams) => println(s"$grams grams")
}
Supports conversion among:
- gram
- kilogram
- meter
- ounce
- pound
Units are as defined as by JSR 363 - see http://jscience.org/api/javax/measure/unit/SI.html and http://jscience.org/api/javax/measure/unit/NonSI.html
Library supports case insensitve parsing of strings into units of measurement, including singular, plural and short hand forms.
Example:
import io.flow.uom.Converter
val converter = Converter()
converter.validateUnitOfMeasurement("mm") match {
case Left(errors) => println(error)
case Right(uom) => println(s"mm maps to $uom")
}
converter.validateUnitOfMass("kg") match {
case Left(errors) => println(error)
case Right(uom) => println(s"kg maps to $uom")
}