Conversion of unit library for golang
forked from martinlindhe/unit
Add more unit including common unit from China
New function UnitConv
to do conversion conveniently
go get -u github.com/foxmeder/unit
Basic usage:
ft := 1 * unit.Foot
fmt.Println(ft.Feet(), "feet is", ft.Meters(), "meters")
To use your own data type, you need to convert to the base unit first (eg Length, Speed etc):
type MyUnit int
n := MyUnit(2)
ft := Length(n) * Foot
fmt.Println(ft.Feet(), "feet is", ft.Meters(), "meters")
Conversion func usage:
from
and to
use format of dimension
.name
,see dimension list for details
For example:use pressure.atm
for Atmosphere
from := "pressure.atm"
to := "pressure.mmhg"
result,err := unit.UnitConv(from, to, 100)
Cannot be used to scale directly like the other units. Instead, use the From* functions to create a Temperature type:
f := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", f.Celsius())
Please note the resulting precision is limited to the float64 type. Big decimal version is being tracked in martinlindhe#3
Under MIT