monetary

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

gomonetary

Build Status codecov

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

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 - 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:

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

Documentation

Overview

Generated by lcgenerator; DO NOT EDIT Last Generated: 2019-03-27 17:56:39.334393 -0500 CDT m=+0.741287822

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(value float64, locale string) (string, error)

func FormatDefault

func FormatDefault(value float64) (string, error)

func GetDefaultLocale

func GetDefaultLocale() string

func GetIsSupported

func GetIsSupported(name string) bool

func GetSupportedLocales

func GetSupportedLocales() []string

func Parse

func Parse(value, locale string) (float64, error)

func ParseDefault

func ParseDefault(value string) (float64, error)

func SetDefaultLocale

func SetDefaultLocale(locale string) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL