money

package
v0.0.0-...-17c8ba4 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package money implements utility routines for manipulating monetary values.

This package is a basic implementation of Google's micros https://developers.google.com/standard-payments/reference/glossary#micros Storing monetary values as integer by multiplying / dividing it by 1,000,000. For example £1.23 is stored as 1230000.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Money

type Money int64

Store monetary values as integer.

func New

func New[T int | int32 | int64 | float64](amount T) Money

New initializes and return a Money. It accepts integer and float types. For float types, he input will be rounded to the nearest integer using math.RoundToEven().

func NewFromString

func NewFromString(amount string) (Money, error)

NewForString initializes and return a Money. It accepts an arbitrary string containing a number. The number is converted to float64 then rounded to the nearest integer using math.RoundToEven(). If the conversion fails, an error is returned.

func (Money) Add

func (m Money) Add(add float64) Money

Add returns a Money with a provided float64 added.

func (Money) DisplayCurrency

func (m Money) DisplayCurrency(sign string) string

DisplayCurrency returns a rounded Money as a string with comma thousands separators.

func (Money) Div

func (m Money) Div(div float64) Money

Div returns a Money divided by a provided float64. If the divisor is 0, the Money will be returned as-is.

func (Money) Format

func (m Money) Format(digits int) string

Format returns a Money as a string with the specified digits. For example 66498000 with digits = 2 is returned as 66.50.

func (Money) Mul

func (m Money) Mul(mul float64) Money

Mul returns a Money multiplied by a provided float64.

func (Money) Sub

func (m Money) Sub(sub float64) Money

Sub returns a Money substracted by a provided float64.

Jump to

Keyboard shortcuts

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