Go utility library to convert strings to Ether values and vice versa.
When converting strings to numeric values the process is case-insensitive and doesn't care about whitespace, so input values such as "0.1 Ether", "0.1Ether" and "0.1ether" would all result in the same result. The standard unit denominations (Wei, Ether) are supported with or without SI prefixes (micro, milli, kilo, mega etc.), as are common names (Shannon, Babbage).
When converting numeric values to strings the user can select standard mode, in which case all values will be in units of Wei or Ether, or non-standard mode, in which case the full range of values will be used.
This also provides the ability to convert between string and GWei values.
go-string2eth
is a standard Go module which can be installed with:
go get github.com/wealdtech/go-string2eth
go-string2eth
converts from strings to Ether values and back again.
package main
import (
string2eth "github.com/wealdtech/go-string2eth"
)
func main() {
// Convert a string value to a number of Wei
value, err := string2eth.StringToWei("0.05 Ether")
if err != nil {
panic(err)
}
// Convert a number of Wei to a string value
str := string2eth.WeiToString(value, true)
fmt.Printf("0.05 Ether is %v Wei, is %s\n", value, str)
}
Jim McDonald: @mcdee.
Contributions welcome. Please check out the issues.
Apache-2.0 © 2019 Weald Technology Trading Ltd