https://github.com/elliotcourant/gomonetary
gomonetary is a pure go package for parsing and formatting monetary values.
https://github.com/elliotcourant/gomonetary
currency finance go go-package golang golang-library golang-package locale localization parser
Last synced: about 1 year ago
JSON representation
gomonetary is a pure go package for parsing and formatting monetary values.
- Host: GitHub
- URL: https://github.com/elliotcourant/gomonetary
- Owner: elliotcourant
- License: apache-2.0
- Created: 2019-03-05T20:10:08.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-11-17T20:42:58.000Z (over 2 years ago)
- Last Synced: 2025-04-04T05:29:25.073Z (about 1 year ago)
- Topics: currency, finance, go, go-package, golang, golang-library, golang-package, locale, localization, parser
- Language: Go
- Homepage:
- Size: 88.9 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gomonetary
[](https://travis-ci.com/elliotcourant/gomonetary)
[](https://codecov.io/gh/elliotcourant/gomonetary)
gomonetary is a pure go implementation to parse and format
currency text. Given a locale it can take a float and format
it according to that locale. It can also take a formatted
string from a locale and parse it into a float.
## Example
```go
package main
import (
"fmt"
"github.com/elliotcourant/gomonetary"
)
func main() {
/*
Formatting
*/
usDollars, _ := monetary.Format(123.56, "en_US")
fmt.Println("Formatted Value:", usDollars)
// Output: Formatted Value: $123.56
ruExample, _ := monetary.Format(5438.98, "ru_RU")
fmt.Println("Formatted Value:", ruExample)
// Output: Formatted Value: 5.438,98 руб.
/*
Parsing
*/
usParsed, _ := monetary.Parse(usDollars, "en_US")
fmt.Println("Parsed Value:", usParsed)
// Output: Parsed Value: 123.56
ruParsed, _ := monetary.Parse(ruExample, "ru_RU")
fmt.Println("Parsed Value:", ruParsed)
// Output: Parsed Value: 5438.98
}
```
## Cache
Monetary information for each locale is cached ([generated.go](generated.go) - cache file), this allows for better
performance but also guarantees support for all OS's for using the
package.
Currently, only Linux and Mac OS X can generate monetary information for
the cache. This uses the unix command `locale` and the data from the host
operating system to populate the cache.
To regenerate the cache on a supported OS run:
```bash
make metadata
```
This will replace the current cache (I will make it additive in the future)
with all of the locale information on your system. Note: On Linux you might
need to add other locales because they are not typically included be default.
Mac OS usually has a couple hundred installed by default.
## Benchmarks
These were run on a 2017 MacBook Pro with a 2.8GHz Intel Core i7 running macOS 10.14.3.
As you can see the format function still needs some optimizations.
```
BenchmarkFormat-8 1000000 1679 ns/op 408 B/op 29 allocs/op
BenchmarkParse-8 5000000 345 ns/op 10 B/op 2 allocs/op
```