money

package
v1.98.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package money represents money values with no loss in precision and without rounding errors.

Index

Constants

This section is empty.

Variables

View Source
var (
	EUR = FormatConfig{
		Symbol:   "\u20ac",
		Thousand: ",",
	}

	USD = FormatConfig{
		Symbol:   "$",
		Prefix:   true,
		Thousand: ",",
	}

	GBP = FormatConfig{
		Symbol:   "\u00a3",
		Prefix:   true,
		Thousand: ",",
	}

	MXN = FormatConfig{
		Symbol:   "Mex$",
		Prefix:   true,
		Thousand: ",",
	}
)

Functions

This section is empty.

Types

type FormatConfig added in v1.55.0

type FormatConfig struct {
	Symbol        string
	Prefix        bool
	Thousand      string
	Decimal       string
	ForceDecimals bool
}

func Currency added in v1.55.0

func Currency(currency string) FormatConfig

Currency returns the configuration of a currency by its name. Unknown currencies will return the default config.

type Money

type Money int32

func FromCents added in v1.55.0

func FromCents(cents int32) Money

FromCents creates a new instance with a cents value.

func Parse

func Parse(s string) (Money, error)

Parse a string to create a new money value. It can read `XX.YY` and `XX,YY`. An empty string is parsed as zero.

func (Money) Add

func (m Money) Add(other Money) Money

Add two money values together and returns the result.

func (Money) AddTaxPercent

func (m Money) AddTaxPercent(tax int32) Money

AddTaxPercent adds a percentage of the price to itself.

func (Money) Cents

func (m Money) Cents() int32

Cents returns the value with cents precision (2 decimal places) as a number.

func (Money) Div

func (m Money) Div(other Money) Money

Div divides two money values and returns the result.

func (Money) Format

func (m Money) Format(config FormatConfig) string

Format the money value with a specific configuration.

func (Money) IsZero

func (m Money) IsZero() bool

IsZero returns true if there is no money.

func (Money) LessThan

func (m Money) LessThan(other Money) bool

LessThan returns true if a money value is less than the other.

func (Money) Mul

func (m Money) Mul(n int32) Money

Mul multiplies the money value n times and returns the result.

func (Money) String added in v1.55.0

func (m Money) String() string

Format the money value with the default configuration

func (Money) Sub

func (m Money) Sub(other Money) Money

Sub subtracts two money values and returns the result.

Jump to

Keyboard shortcuts

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