money

package module
v0.0.0-...-ed01125 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT Imports: 7 Imported by: 0

README

go-money

Package money provides a way of dealing with money in type safe way, including parsing from strings.

So, for example, the type of U.S. Dollars is money.USD and the type for Canadian Dollars is money.CAD. These two types are completely different, and cannot be assigned to each other.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-money

GoDoc

Parsing

To turn a money amount represented as a string into a money type, do something like the follow.

canadianMoney, err := money.ParseCAD("$123.45")

And: usMoney, err := money.ParseUSD("$123.45")

Initializing

There are a number of functions available to initalize the value of a money.USD or a money.CAD:

twentyThreeCanadianCents := money.CADCents(23)
twentyThreeUSCents       := money.USDCents(23)

fiftyThreeCanadianDollars := money.CADCents(53)
fiftyThreeUSDollars       := money.USDCents(53)

Common Money Amounts

There are some convenience functions for common money amounts.

For Canadian Dollars there is:

oneCentCAD            := money.CADCent()

fiveCentsCAD          := money.CADNickel()

tenCentsCAD           := money.CADDime()

twentyFiveCentsCAD    := money.CADQuarter()

twentyFiveCentsCADToo := money.CADTwoBits()

fiftyCentsCAD         := money.CADFourBits()

seventyFiveCentsCAD   := money.CADSixBits()

oneHundredCentsCAD    := money.CADEightBits()

oneHundredCentsCADToo := money.CADDollar()

And for U.S. Dollars there is:

oneCentUSD            := money.USDCent()

fiveCentsUSD          := money.USDNickel()

tenCentsUSD           := money.USDDime()

twentyFiveCentsUSD    := money.USDQuarter()

twentyFiveCentsUSDToo := money.USDTwoBits()

fiftyCentsUSD         := money.USDFourBits()

seventyFiveCentsUSD   := money.USDSixBits()

oneHundredCentsUSD    := money.USDEightBits()

oneHundredCentsUSDToo := money.USDDollar()

Formatting

To turn a money amount into a specific string format, do something like the following.

strCAD := canadianMoney.Format("")           // $123.45

strCAD := canadianMoney.Format("$")          // $123.45

strCAD := canadianMoney.Format("\uFE69")     // ﹩123.45
strCAD := canadianMoney.Format("﹩")          // ﹩123.45

strCAD := canadianMoney.Format("\uFF04")     // $123.45
strCAD := canadianMoney.Format("$")         // $123.45

strCAD := canadianMoney.Format("\U0001F4B2") // 💲123.45
strCAD := canadianMoney.Format("💲")         // 💲123.45

strCAD := canadianMoney.Format("\u00A2")     // 12345¢
strCAD := canadianMoney.Format("¢")          // 12345¢

strCAD := canadianMoney.Format("\uFFE0")     // 12345¢
strCAD := canadianMoney.Format("¢")         // 12345¢


strUSD := usMoney.Format("")           // $123.45

strUSD := usMoney.Format("$")          // $123.45

strUSD := usMoney.Format("\uFE69")     // ﹩123.45
strUSD := usMoney.Format("﹩")          // ﹩123.45

strUSD := usMoney.Format("\uFF04")     // $123.45
strUSD := usMoney.Format("$")         // $123.45

strUSD := usMoney.Format("\U0001F4B2") // 💲123.45
strUSD := usMoney.Format("💲")         // 💲123.45

strUSD := usMoney.Format("\u00A2")     // 12345¢
strUSD := usMoney.Format("¢")          // 12345¢

strUSD := usMoney.Format("\uFFE0")     // 12345¢
strUSD := usMoney.Format("¢")         // 12345¢

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CAD

type CAD struct {
	// contains filtered or unexported fields
}

CAD represents a Canadian Dollar amount.

Note that CAD is an option-type, and thus its uninitialized form is nothing (and not zero).

func CADCanonicalForm

func CADCanonicalForm(dollars int32, cents int8) CAD

CADCanonicalForm takes a dollars, and cents amount and returns a money.CAD with that amount.

For example:

var value money.CAD = money.CADCanonicalForm(123, 45)

This will return $123.45

func CADCent

func CADCent() CAD

func CADCents

func CADCents(value int64) CAD

func CADDime

func CADDime() CAD

func CADDollar

func CADDollar() CAD

func CADDollars

func CADDollars(value int64) CAD

func CADEightBits

func CADEightBits() CAD

CADEightBits returns eight-bits in Canadian Dollars.

Eight-bits means 100¢ (i.e., $1.00).

A bit means ⅛ of a dollar. And thus eight-bits is 1 dollar (i.e., 100¢ or $1.00).

func CADFourBits

func CADFourBits() CAD

USDFourBits returns four-bits in U.S. Dollars.

Four-bits means 50¢ (i.e., $0.50).

A bit means ⅛ of a dollar. And thus four-bits is ½ of a dollar (i.e., 50¢ or $0.50).

func CADNickel

func CADNickel() CAD

func CADQuarter

func CADQuarter() CAD

func CADSixBits

func CADSixBits() CAD

CADSixBits returns six-bits in Canadian Dollars.

Six-bits means 75¢ (i.e., $0.75).

A bit means ⅛ of a dollar. And thus six-bits is ¾ of a dollar (i.e., 75¢ or $0.75).

func CADTwoBits

func CADTwoBits() CAD

CADTwoBits returns two-bits in Canadian Dollars.

Two-bits means 25¢ (i.e., $0.25).

A bit means ⅛ of a dollar. And thus two-bits is ¼ of a dollar (i.e., 25¢ or $0.25).

func MustParseCAD

func MustParseCAD(str string) CAD

MustParseCAD converts a string to a CAD, but panic()s if there is an error.

func ParseCAD

func ParseCAD(str string) (CAD, error)

ParseCAD converts a string to a CAD.

func (CAD) Abs

func (m CAD) Abs() CAD

Abs returns the absolute value.

func (CAD) Add

func (m CAD) Add(val CAD) CAD

func (CAD) CanonicalForm

func (m CAD) CanonicalForm() (dollars int64, cents int64, something bool)

func (CAD) Format

func (m CAD) Format(layout string) string

Format returns a textual representation of the CAD money value formatted according to layout.

Options for layout include:

"$"

$123.45

"\uFE69" or "﹩"

﹩123.45

"\uFF04" or "$"

$123.45

"\U0001F4B2" or "💲"

💲123.45

"\u00A2" or "¢"

12345¢

"\uFFE0" or "¢"

12345¢

"-12345.67"

-9876543.21

"-12345,67"

-9876543,21

"-12,345.67"

-9,876,543.21

"-12.345,67"

-9.876.543,21

"-1234567"

-987654321

"-1,234,567"

-987,654,321

"-1.234.567"

-987.654.321

func (CAD) GoString

func (m CAD) GoString() string

GoString returns a money.CAD in a Go code representation; ex: "money.USDCents(1234)".

func (CAD) MarshalJSON

func (m CAD) MarshalJSON() ([]byte, error)

MarshalJSON is to make it so money.CAD fits the json.Marshaler interface.

func (CAD) Mul

func (m CAD) Mul(scalar int64) CAD

func (*CAD) Scan

func (m *CAD) Scan(src interface{}) error

Scan is to make it so *money.CAD fits the sql.Scanner interface.

func (CAD) String

func (m CAD) String() string

String returns a string representing the CAD money in the form "$1.23".

func (CAD) Sub

func (m CAD) Sub(other CAD) CAD

func (*CAD) UnmarshalJSON

func (m *CAD) UnmarshalJSON(src []byte) error

UnmarshalJSON is to make it so money.CAD fits the json.Marshaler interface.

func (CAD) Value

func (m CAD) Value() (driver.Value, error)

Value is to make it so money.CAD fits the database/sql/driver.Valuer interface.

type USD

type USD struct {
	// contains filtered or unexported fields
}

USD represents a U.S. Dollar amount.

Note that USD is an option-type, and thus its uninitialized form is nothing (and not zero).

func MustParseUSD

func MustParseUSD(str string) USD

MustParseUSD converts a string to a USD, but panic()s if there is an error.

func ParseUSD

func ParseUSD(str string) (USD, error)

ParseUSD converts a string to a USD.

func USDCanonicalForm

func USDCanonicalForm(dollars int32, cents int8) USD

USDCanonicalForm takes a dollars, and cents amount and returns a money.USD with that amount.

For example:

var value money.USD = money.USDCanonicalForm(123, 45)

This will return $123.45

func USDCent

func USDCent() USD

func USDCents

func USDCents(value int64) USD

func USDDime

func USDDime() USD

func USDDollar

func USDDollar() USD

func USDDollars

func USDDollars(value int64) USD

func USDEightBits

func USDEightBits() USD

USDEightBits returns eight-bits in U.S. Dollars.

Eight-bits means 100¢ (i.e., $1.00).

A bit means ⅛ of a dollar. And thus eight-bits is 1 dollar (i.e., 100¢ or $1.00).

func USDFourBits

func USDFourBits() USD

CADFourBits returns four-bits in U.S. Dollars.

Four-bits means 50¢ (i.e., $0.50).

A bit means ⅛ of a dollar. And thus four-bits is ½ of a dollar (i.e., 50¢ or $0.50).

func USDNickel

func USDNickel() USD

func USDQuarter

func USDQuarter() USD

func USDSixBits

func USDSixBits() USD

USDSixBits returns six-bits in U.S. Dollars.

Six-bits means 75¢ (i.e., $0.75).

A bit means ⅛ of a dollar. And thus six-bits is ¾ of a dollar (i.e., 75¢ or $0.75).

func USDTwoBits

func USDTwoBits() USD

CADTwoBits returns two-bits in U.S. Dollars.

Two-bits means 25¢ (i.e., $0.25).

A bit means ⅛ of a dollar. And thus two-bits is ¼ of a dollar (i.e., 25¢ or $0.25).

func (USD) Abs

func (m USD) Abs() USD

Abs returns the absolute value.

func (USD) Add

func (m USD) Add(val USD) USD

func (USD) CanonicalForm

func (m USD) CanonicalForm() (dollars int64, cents int64, something bool)

func (USD) Format

func (m USD) Format(layout string) string

Format returns a textual representation of the USD money value formatted according to layout.

Options for layout include:

"$"

$123.45

"\uFE69" or "﹩"

﹩123.45

"\uFF04" or "$"

$123.45

"\U0001F4B2" or "💲"

💲123.45

"\u00A2" or "¢"

12345¢

"\uFFE0" or "¢"

12345¢

"-12345.67"

-9876543.21

"-12345,67"

-9876543,21

"-12,345.67"

-9,876,543.21

"-12.345,67"

-9.876.543,21

"-1234567"

-987654321

"-1,234,567"

-987,654,321

"-1.234.567"

-987.654.321

func (USD) GoString

func (m USD) GoString() string

GoString returns a money.USD in a Go code representation; ex: "money.USDCents(1234)".

func (USD) MarshalJSON

func (m USD) MarshalJSON() ([]byte, error)

MarshalJSON is to make it so money.USD fits the json.Marshaler interface.

func (USD) Mul

func (m USD) Mul(scalar int64) USD

func (*USD) Scan

func (m *USD) Scan(src interface{}) error

Scan is to make it so *money.USD fits the sql.Scanner interface.

func (USD) String

func (m USD) String() string

String returns a string representing the USD money in the form "$1.23".

func (USD) Sub

func (m USD) Sub(other USD) USD

func (*USD) UnmarshalJSON

func (m *USD) UnmarshalJSON(src []byte) error

UnmarshalJSON is to make it so money.USD fits the json.Marshaler interface.

func (USD) Value

func (m USD) Value() (driver.Value, error)

Value is to make it so money.USD fits the database/sql/driver.Valuer interface.

Jump to

Keyboard shortcuts

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