money

package
v0.0.0-...-e2cc455 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 13 Imported by: 4

Documentation

Index

Constants

View Source
const (
	AED = "AED" // United Arab Emirates Dirham
	AFN = "AFN" // Afghanistan Afghani
	ALL = "ALL" // Albania Lek
	AMD = "AMD" // Armenia Dram
	ANG = "ANG" // Netherlands Antilles Guilder
	AOA = "AOA" // Angola Kwanza
	ARS = "ARS" // Argentina Peso
	AUD = "AUD" // Australia Dollar
	AWG = "AWG" // Aruba Guilder
	AZN = "AZN" // Azerbaijan New Manat
	BAM = "BAM" // Bosnia and Herzegovina Convertible Marka
	BBD = "BBD" // Barbados Dollar
	BDT = "BDT" // Bangladesh Taka
	BGN = "BGN" // Bulgaria Lev
	BHD = "BHD" // Bahrain Dinar
	BIF = "BIF" // Burundi Franc
	BMD = "BMD" // Bermuda Dollar
	BND = "BND" // Brunei Darussalam Dollar
	BOB = "BOB" // Bolivia Bolíviano
	BRL = "BRL" // Brazil Real
	BSD = "BSD" // Bahamas Dollar
	BTN = "BTN" // Bhutan Ngultrum
	BWP = "BWP" // Botswana Pula
	BYN = "BYN" // Belarus Ruble
	BZD = "BZD" // Belize Dollar
	CAD = "CAD" // Canada Dollar
	CDF = "CDF" // Congo/Kinshasa Franc
	CHF = "CHF" // Switzerland Franc
	CLP = "CLP" // Chile Peso
	CNY = "CNY" // China Yuan Renminbi
	COP = "COP" // Colombia Peso
	CRC = "CRC" // Costa Rica Colon
	CUC = "CUC" // Cuba Convertible Peso
	CUP = "CUP" // Cuba Peso
	CVE = "CVE" // Cape Verde Escudo
	CZK = "CZK" // Czech Republic Koruna
	DJF = "DJF" // Djibouti Franc
	DKK = "DKK" // Denmark Krone
	DOP = "DOP" // Dominican Republic Peso
	DZD = "DZD" // Algeria Dinar
	EGP = "EGP" // Egypt Pound
	ERN = "ERN" // Eritrea Nakfa
	ETB = "ETB" // Ethiopia Birr
	EUR = "EUR" // Euro Member Countries
	FJD = "FJD" // Fiji Dollar
	FKP = "FKP" // Falkland Islands (Malvinas) Pound
	GBP = "GBP" // United Kingdom Pound
	GEL = "GEL" // Georgia Lari
	GGP = "GGP" // Guernsey Pound
	GHS = "GHS" // Ghana Cedi
	GIP = "GIP" // Gibraltar Pound
	GMD = "GMD" // Gambia Dalasi
	GNF = "GNF" // Guinea Franc
	GTQ = "GTQ" // Guatemala Quetzal
	GYD = "GYD" // Guyana Dollar
	HKD = "HKD" // Hong Kong Dollar
	HNL = "HNL" // Honduras Lempira
	HRK = "HRK" // Croatia Kuna
	HTG = "HTG" // Haiti Gourde
	HUF = "HUF" // Hungary Forint
	IDR = "IDR" // Indonesia Rupiah
	ILS = "ILS" // Israel Shekel
	IMP = "IMP" // Isle of Man Pound
	INR = "INR" // India Rupee
	IQD = "IQD" // Iraq Dinar
	IRR = "IRR" // Iran Rial
	ISK = "ISK" // Iceland Krona
	JEP = "JEP" // Jersey Pound
	JMD = "JMD" // Jamaica Dollar
	JOD = "JOD" // Jordan Dinar
	JPY = "JPY" // Japan Yen
	KES = "KES" // Kenya Shilling
	KGS = "KGS" // Kyrgyzstan Som
	KHR = "KHR" // Cambodia Riel
	KMF = "KMF" // Comoros Franc
	KPW = "KPW" // Korea (North) Won
	KRW = "KRW" // Korea (South) Won
	KWD = "KWD" // Kuwait Dinar
	KYD = "KYD" // Cayman Islands Dollar
	KZT = "KZT" // Kazakhstan Tenge
	LAK = "LAK" // Laos Kip
	LBP = "LBP" // Lebanon Pound
	LKR = "LKR" // Sri Lanka Rupee
	LRD = "LRD" // Liberia Dollar
	LSL = "LSL" // Lesotho Loti
	LYD = "LYD" // Libya Dinar
	MAD = "MAD" // Morocco Dirham
	MDL = "MDL" // Moldova Leu
	MGA = "MGA" // Madagascar Ariary
	MKD = "MKD" // Macedonia Denar
	MMK = "MMK" // Myanmar (Burma) Kyat
	MNT = "MNT" // Mongolia Tughrik
	MOP = "MOP" // Macau Pataca
	MRO = "MRO" // Mauritania Ouguiya
	MUR = "MUR" // Mauritius Rupee
	MVR = "MVR" // Maldives (Maldive Islands) Rufiyaa
	MWK = "MWK" // Malawi Kwacha
	MXN = "MXN" // Mexico Peso
	MYR = "MYR" // Malaysia Ringgit
	MZN = "MZN" // Mozambique Metical
	NAD = "NAD" // Namibia Dollar
	NGN = "NGN" // Nigeria Naira
	NIO = "NIO" // Nicaragua Cordoba
	NOK = "NOK" // Norway Krone
	NPR = "NPR" // Nepal Rupee
	NZD = "NZD" // New Zealand Dollar
	OMR = "OMR" // Oman Rial
	PAB = "PAB" // Panama Balboa
	PEN = "PEN" // Peru Sol
	PGK = "PGK" // Papua New Guinea Kina
	PHP = "PHP" // Philippines Peso
	PKR = "PKR" // Pakistan Rupee
	PLN = "PLN" // Poland Zloty
	PYG = "PYG" // Paraguay Guarani
	QAR = "QAR" // Qatar Riyal
	RON = "RON" // Romania New Leu
	RSD = "RSD" // Serbia Dinar
	RUB = "RUB" // Russia Ruble
	RWF = "RWF" // Rwanda Franc
	SAR = "SAR" // Saudi Arabia Riyal
	SBD = "SBD" // Solomon Islands Dollar
	SCR = "SCR" // Seychelles Rupee
	SDG = "SDG" // Sudan Pound
	SEK = "SEK" // Sweden Krona
	SGD = "SGD" // Singapore Dollar
	SHP = "SHP" // Saint Helena Pound
	SLL = "SLL" // Sierra Leone Leone
	SOS = "SOS" // Somalia Shilling
	SPL = "SPL" // Seborga Luigino
	SRD = "SRD" // Suriname Dollar
	STD = "STD" // São Tomé and Príncipe Dobra
	SVC = "SVC" // El Salvador Colon
	SYP = "SYP" // Syria Pound
	SZL = "SZL" // Swaziland Lilangeni
	THB = "THB" // Thailand Baht
	TJS = "TJS" // Tajikistan Somoni
	TMT = "TMT" // Turkmenistan Manat
	TND = "TND" // Tunisia Dinar
	TOP = "TOP" // Tonga Pa'anga
	TRY = "TRY" // Turkey Lira
	TTD = "TTD" // Trinidad and Tobago Dollar
	TVD = "TVD" // Tuvalu Dollar
	TWD = "TWD" // Taiwan New Dollar
	TZS = "TZS" // Tanzania Shilling
	UAH = "UAH" // Ukraine Hryvnia
	UGX = "UGX" // Uganda Shilling
	USD = "USD" // United States Dollar
	UYU = "UYU" // Uruguay Peso
	UZS = "UZS" // Uzbekistan Som
	VEF = "VEF" // Venezuela Bolivar
	VND = "VND" // Viet Nam Dong
	VUV = "VUV" // Vanuatu Vatu
	WST = "WST" // Samoa Tala
	XAF = "XAF" // Communauté Financière Africaine (BEAC) CFA Franc BEAC
	XCD = "XCD" // East Caribbean Dollar
	XDR = "XDR" // International Monetary Fund (IMF) Special Drawing Rights
	XOF = "XOF" // Communauté Financière Africaine (BCEAO) Franc
	XPF = "XPF" // Comptoirs Français du Pacifique (CFP) Franc
	YER = "YER" // Yemen Rial
	ZAR = "ZAR" // South Africa Rand
	ZMW = "ZMW" // Zambia Kwacha
	ZWD = "ZWD" // Zimbabwe Dollar

	BTC = "BTC" // Bitcoin
)
View Source
const CurrencyNull = ""

CurrencyNull represents the SQL NULL for Currency and NullableCurrency. Currency(CurrencyNull).Valid() == false NullableCurrency(CurrencyNull).Valid() == true

Variables

View Source
var AmountFinder amountFinder

Functions

func StringIsAmount

func StringIsAmount(str string, acceptInt bool) bool

StringIsAmount returns if str can be parsed as Amount.

func StringIsCurrency

func StringIsCurrency(str string) bool

StringIsCurrency returns if a string can be parsed as Currency.

Types

type Amount

type Amount float64

Amount adds money related methods to float64

func AmountFromPtr

func AmountFromPtr(ptr *Amount, defaultVal Amount) Amount

AmountFromPtr dereferences ptr or returns defaultVal if it is nil

func NewAmount

func NewAmount(value float64) *Amount

NewAmount returns a pointer to an Amount with the passed value.

func ParseAmount

func ParseAmount(str string, acceptedDecimals ...int) (Amount, error)

ParseAmount parses an amount from str accepting only certain decimal digit counts. If no acceptedDecimals are passed, then any decimal digit count is accepted. Infinity and NaN are parsed and returned without error. The Amount.Valid method can be aused to check for infinity and NaN.

func (Amount) Abs

func (a Amount) Abs() Amount

Abs returns the absolute value of a.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

func (Amount) AbsFloat

func (a Amount) AbsFloat() float64

AbsFloat returns the absolute value of a as float64.

Special cases are:

AbsFloat(±Inf) = +Inf
AbsFloat(NaN) = NaN

func (*Amount) AmountOr

func (ptr *Amount) AmountOr(defaultVal Amount) Amount

AmountOr returns the pointed to amount or defaultVal if ptr is nil.

func (Amount) BigFloat

func (a Amount) BigFloat() *big.Float

BigFloat returns m as a new big.Float

func (Amount) Cents

func (a Amount) Cents() int64

Cents returns the amount rounded to cents

func (Amount) Copysign

func (a Amount) Copysign(sign Amount) Amount

Copysign returns an Amount with the magnitude of a and with the sign of the sign argument.

func (Amount) DividedByRate

func (a Amount) DividedByRate(rate Rate) Amount

DividedByRate returns the amount divided by a rate.

func (*Amount) Equal

func (a *Amount) Equal(b *Amount) bool

Equal returns if two Amount pointers point to Amounts with equal values or equal addresses.

func (*Amount) FloatOr

func (ptr *Amount) FloatOr(defaultVal float64) float64

FloatOr returns the pointed to amount as float64 or defaultVal if ptr is nil.

func (Amount) Format

func (a Amount) Format(thousandsSep, decimalSep rune, precision int) string

Format formats the Amount similar to strconv.FormatFloat with the 'f' format option, but with decimalSep as decimal separator instead of a point and optional grouping of the integer part. Valid values for decimalSep are '.' and ','. If thousandsSep is not zero, then the integer part of the number is grouped with thousandsSep between every group of 3 digits. Valid values for thousandsSep are [0, ',', '.'] and thousandsSep must be different from decimalSep. The precision argument controls the number of digits (excluding the exponent). Note that the last digit is not rounded! The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly.

func (Amount) GoString

func (a Amount) GoString() string

GoString returns the amount as string in full float64 precision for debugging

func (*Amount) Invert

func (a *Amount) Invert()

Invert inverts the sign of the amount.

func (Amount) Inverted

func (a Amount) Inverted() Amount

Inverted returns the amount with inverted sign.

func (Amount) IsInf

func (a Amount) IsInf() bool

IsNaN returns if the amount is positive or negative infinity

func (Amount) IsNaN

func (a Amount) IsNaN() bool

IsNaN returns if the amount is not a number (NaN)

func (Amount) MultipliedByRate

func (a Amount) MultipliedByRate(rate Rate) Amount

MultipliedByRate returns the amount multiplied by a rate.

func (Amount) Percentage

func (a Amount) Percentage(percent float64) Amount

Percentage returns the amount multiplied by (percent / 100).

func (Amount) RoundToCents

func (a Amount) RoundToCents() Amount

RoundToCents returns the amount rounded to cents (2 decimal places)

func (Amount) RoundToDecimals

func (a Amount) RoundToDecimals(decimals int) Amount

RoundToDecimals returns the amount rounded to the passed number of decimal places.

func (Amount) RoundToInt

func (a Amount) RoundToInt() Amount

RoundToInt returns the amount rounded to an integer number

func (*Amount) ScanString

func (a *Amount) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, then the Amount.Valid method is checked to return an error if the floating point value is infinite or NaN.

func (Amount) Signbit

func (a Amount) Signbit() bool

Signbit reports whether a is negative or negative zero.

func (Amount) SplitEqually

func (a Amount) SplitEqually(count int) []Amount

SplitEqually divides the amount equally into count amounts that are rounded to cents and sum up to the initial amount rounded to cents. The last amount may slightly differ from the others amounts to guarantee that the sum of the rounded cents equals the rounded cents of the initial amount.

func (Amount) SplitProportionally

func (a Amount) SplitProportionally(weights []Amount) []Amount

SplitProportionally splits an amount proportianly to the passed weights into the same number of amounts that get round to cents and makes sure that the sum is identical to the amount rounded to cents. The passed weights can be positive, negative, or zero, so the result amounts can also be positive, negative, or zero.

func (Amount) String

func (a Amount) String() string

String returns the amount rounded to two decimal places formatted with a dot as decimal separator. String implements the fmt.Stringer interface.

func (*Amount) StringOr

func (ptr *Amount) StringOr(defaultVal string) string

StringOr returns ptr.String() or defaultVal if ptr is nil.

func (*Amount) UnmarshalJSON

func (a *Amount) UnmarshalJSON(j []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler and accepts numbers, strings, and null. JSON null and "" will set the amout to zero.

func (Amount) Valid

func (a Amount) Valid() bool

Valid returns if the amount is neither infinite nor NaN

func (Amount) ValidAndGreaterZero

func (a Amount) ValidAndGreaterZero() bool

ValidAndGreaterZero returns if the amount is neither infinite nor NaN and greater than zero.

func (Amount) ValidAndHasSign

func (a Amount) ValidAndHasSign(sign int) bool

ValidAndHasSign returns if a.Valid() and if it has the same sign than the passed int argument or any sign if 0 is passed.

func (Amount) ValidAndPositive

func (a Amount) ValidAndPositive() bool

ValidAndPositive returns if the amount is neither infinite nor NaN and greater than or equal zero.

func (Amount) ValidAndSmallerZero

func (a Amount) ValidAndSmallerZero() bool

ValidAndSmallerZero returns if the amount is neither infinite nor NaN and smaller than zero.

func (Amount) WithNegSign

func (a Amount) WithNegSign(negative bool) Amount

WithNegSign returns the amount with a negative sign if true is passed, or with a positive sign (abs) if false is passed.

func (Amount) WithPosSign

func (a Amount) WithPosSign(positive bool) Amount

WithPosSign returns the amount with a positive sign (abs) if true is passed, or with a negative sign if false is passed.

type AmountParser

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

AmountParser implements the strfmt.Parser interface for money amounts.

func NewAmountParser

func NewAmountParser(acceptedDecimals ...int) *AmountParser

func (*AmountParser) Parse

func (p *AmountParser) Parse(str string, langHints ...language.Code) (normalized string, err error)

type Currency

type Currency string

Currency is holds a 3 character ISO 4217 alphabetic code. Currency implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and will treat an empty Currency string as SQL NULL value. The main difference between Currency and NullableCurrency is: Currency("").Valid() == false NullableCurrency("").Valid() == true

func NormalizeCurrency

func NormalizeCurrency(str string) (Currency, error)

NormalizeCurrency returns str as normalized Currency or an error.

func (Currency) EnglishName

func (c Currency) EnglishName() string

EnglishName returns the english name of the currency

func (*Currency) GetOrDefault

func (c *Currency) GetOrDefault(defaultVal Currency) Currency

GetOrDefault returns the value c references if it is valid and c is not nil. Safe to call on a nil pointer.

func (Currency) IsEUR

func (c Currency) IsEUR() bool

IsEUR returns if the currency can be normalized as Euro.

func (Currency) Normalized

func (c Currency) Normalized() (Currency, error)

Normalized normalizes a currency string

func (Currency) NormalizedOrNull

func (c Currency) NormalizedOrNull() NullableCurrency

NormalizedOrNull returns the normalized currency or CurrencyNull if it was not a valid currency.

func (Currency) NullableCurrency

func (c Currency) NullableCurrency() NullableCurrency

NullableCurrency returns c as NullableCurrency where NullableCurrency("").Valid() == true

func (*Currency) Scan

func (c *Currency) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*Currency) ScanString

func (c *Currency) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, the source string is checked for validity before it is assigned to the type.

If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.

func (Currency) String

func (c Currency) String() string

String returns the normalized currency as string if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.

func (Currency) Symbol

func (c Currency) Symbol() string

Symbol returns the currency symbol like € for EUR if available, or currency code if no widely recognized symbol is available.

func (Currency) Valid

func (c Currency) Valid() bool

Valid indicates if c can be normalized to a valid currency.

func (*Currency) ValidPtr

func (c *Currency) ValidPtr() bool

ValidPtr returns if c is not nil and can be normalized to a valid currency. Safe to call on a nil pointer.

func (Currency) Validate

func (c Currency) Validate() error

Validate returns an error if c can not be normalized to a valid currency.

func (Currency) Value

func (c Currency) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface.

type CurrencyAmount

type CurrencyAmount struct {
	Currency Currency
	Amount   Amount
}

func NewCurrencyAmount

func NewCurrencyAmount(currency Currency, amount Amount) CurrencyAmount

func ParseCurrencyAmount

func ParseCurrencyAmount(str string, acceptedDecimals ...int) (result CurrencyAmount, err error)

ParseCurrencyAmount parses a currency and an amount from str with acceptedDecimals. If acceptedDecimals is empty, then any decimal number is accepted.

func (CurrencyAmount) Format

func (ca CurrencyAmount) Format(currencyFirst bool, thousandsSep, decimalSep rune, precision int) string

func (CurrencyAmount) GoString

func (ca CurrencyAmount) GoString() string

func (*CurrencyAmount) Scan

func (ca *CurrencyAmount) Scan(value any) (err error)

Scan implements the database/sql.Scanner interface using ParseCurrencyAmount.

func (*CurrencyAmount) ScanString

func (ca *CurrencyAmount) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, then the Amount.Valid method is checked to return an error if the floating point value is infinite or NaN.

func (CurrencyAmount) String

func (ca CurrencyAmount) String() string

String implements the fmt.Stringer interface.

func (CurrencyAmount) Value

func (ca CurrencyAmount) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface by returning the result of the String method.

type CurrencyAmountParser

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

CurrencyAmountParser implements the strfmt.Parser interface for money amounts.

func NewCurrencyAmountParser

func NewCurrencyAmountParser(acceptedDecimals ...int) *CurrencyAmountParser

func (*CurrencyAmountParser) Parse

func (p *CurrencyAmountParser) Parse(str string, langHints ...language.Code) (normalized string, err error)

type CurrencyParser

type CurrencyParser struct{}

CurrencyParser implements the strfmt.Parser interface for dates.

func (CurrencyParser) Parse

func (CurrencyParser) Parse(str string, langHints ...language.Code) (normalized string, err error)

type NullableCurrency

type NullableCurrency string

NullableCurrency holds a 3 character ISO 4217 alphabetic code, or an empty string as valid value representing NULL in SQL databases. NullableCurrency implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and will treat an empty NullableCurrency string as SQL NULL value. The main difference between Currency and NullableCurrency is: Currency("").Valid() == false NullableCurrency("").Valid() == true

func (NullableCurrency) Currency

func (n NullableCurrency) Currency() Currency

func (NullableCurrency) EnglishName

func (n NullableCurrency) EnglishName() string

EnglishName returns the english name of the currency

func (NullableCurrency) Get

func (n NullableCurrency) Get() Currency

Get returns the non nullable Currency value or panics if the NullableCurrency is null. Note: check with IsNull before using Get!

func (NullableCurrency) GetOr

func (n NullableCurrency) GetOr(defaultVal Currency) Currency

GetOr returns the Currency if n valid and not null, or else the passed defaultVal is returned.

func (NullableCurrency) IsNotNull

func (n NullableCurrency) IsNotNull() bool

IsNotNull returns true if the NullableCurrency is not null.

func (NullableCurrency) IsNull

func (n NullableCurrency) IsNull() bool

IsNull returns true if the NullableCurrency is null. IsNull implements the nullable.Nullable interface.

func (NullableCurrency) MarshalJSON

func (n NullableCurrency) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.

func (NullableCurrency) Normalized

func (n NullableCurrency) Normalized() (NullableCurrency, error)

Normalized normalizes a currency string

func (NullableCurrency) NormalizedOrNull

func (n NullableCurrency) NormalizedOrNull() NullableCurrency

NormalizedOrNull returns a normalized currency or CurrencyNull if there was an error while normalizing.

func (*NullableCurrency) Scan

func (n *NullableCurrency) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*NullableCurrency) ScanString

func (n *NullableCurrency) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, the source string is checked for validity before it is assigned to the type.

If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.

func (*NullableCurrency) Set

func (n *NullableCurrency) Set(currency Currency)

Set sets an ID for this NullableCurrency

func (*NullableCurrency) SetNull

func (n *NullableCurrency) SetNull()

SetNull sets the NullableCurrency to null

func (NullableCurrency) String

func (n NullableCurrency) String() string

String returns the normalized currency as string if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.

func (NullableCurrency) StringOr

func (n NullableCurrency) StringOr(defaultString string) string

StringOr returns the NullableCurrency as string or the passed defaultString if the NullableCurrency is null.

func (NullableCurrency) Symbol

func (n NullableCurrency) Symbol() string

Symbol returns the currency symbol like € for EUR if available, or currency code if no widely recognized symbol is available.

func (NullableCurrency) Valid

func (n NullableCurrency) Valid() bool

Valid returns true if n is an empty string, or can be normalized to a valid currency.

func (NullableCurrency) ValidAndNotNull

func (n NullableCurrency) ValidAndNotNull() bool

ValidAndNotNull returns if the currency is valid and not Null.

func (*NullableCurrency) ValidPtr

func (n *NullableCurrency) ValidPtr() bool

Valid returns true if c is nil, an empty string, or a valid 3 character ISO 4217 alphabetic code. Safe to call on a nil pointer.

func (NullableCurrency) Validate

func (n NullableCurrency) Validate() error

Validate returns an error if n is not an empty string and can not be normalized to a valid currency.

func (NullableCurrency) Value

func (n NullableCurrency) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface.

type Rate

type Rate float64

Rate is a float64 underneath with additional methods useful for money conversion rates and percentages.

func NewRate

func NewRate(value float64) *Rate

NewRate returns a pointer to a Rate with the passed value.

func ParseRate

func ParseRate(str string, acceptedDecimals ...int) (Rate, error)

ParseRate parses a rate from str accepting only certain decimal digit counts. If no acceptedDecimals are passed, then any decimal digit count is accepted. If a string ends with '%' then the parsed number part will be divided by 100.

func RateFromPtr

func RateFromPtr(ptr *Rate, defaultVal Rate) Rate

RateFromPtr dereferences ptr or returns defaultVal if it is nil

func (Rate) Abs

func (r Rate) Abs() Rate

Abs returns the absolute value of r.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

func (Rate) AbsFloat

func (r Rate) AbsFloat() float64

AbsFloat returns the absolute value of r as float64.

Special cases are:

AbsFloat(±Inf) = +Inf
AbsFloat(NaN) = NaN

func (Rate) BigFloat

func (r Rate) BigFloat() *big.Float

BigFloat returns m as a new big.Float

func (Rate) Copysign

func (r Rate) Copysign(sign Rate) Rate

Copysign returns an Rate with the magnitude of a and with the sign of the sign argument.

func (*Rate) Equal

func (r *Rate) Equal(other *Rate) bool

func (*Rate) FloatOr

func (ptr *Rate) FloatOr(defaultVal float64) float64

FloatOr returns the pointed to rate as float64 or defaultVal if ptr is nil.

func (Rate) Format

func (r Rate) Format(thousandsSep, decimalSep rune, precision int) string

Format formats the Rate similar to strconv.FormatFloat with the 'f' format option, but with decimalSep as decimal separator instead of a point and optional grouping of the integer part. Valid values for decimalSep are '.' and ','. If thousandsSep is not zero, then the integer part of the number is grouped with thousandsSep between every group of 3 digits. Valid values for thousandsSep are [0, ',', '.'] and thousandsSep must be different from decimalSep. The precision argument controls the number of digits (excluding the exponent). Note that the last digit is not rounded! The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly.

func (Rate) GoString

func (r Rate) GoString() string

GoString returns the rate as string in full float64 precision for debugging

func (Rate) Inverse

func (r Rate) Inverse() Rate

Inverse returns the inverse rate (1 / r)

func (*Rate) Invert

func (r *Rate) Invert()

Invert inverts the sign of the rate.

func (Rate) Inverted

func (r Rate) Inverted() Rate

Inverted returns the rate with inverted sign.

func (*Rate) RateOr

func (ptr *Rate) RateOr(defaultVal Rate) Rate

RateOr returns the pointed to rate or defaultVal if ptr is nil.

func (Rate) RoundToDecimals

func (r Rate) RoundToDecimals(decimals int) Rate

RoundToDecimals returns the value rounded to the passed number of decimal places.

func (Rate) RoundToInt

func (r Rate) RoundToInt() Rate

RoundToInt returns the value rounded to an integer number

func (*Rate) ScanString

func (r *Rate) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, the source string is checked for validity before it is assigned to the type.

If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.

func (Rate) Signbit

func (r Rate) Signbit() bool

Signbit reports whether a is negative or negative zero.

func (*Rate) StringOr

func (ptr *Rate) StringOr(defaultVal string) string

StringOr returns strconv.FormatFloat the pointed to rate or defaultVal if ptr is nil.

func (*Rate) UnmarshalJSON

func (r *Rate) UnmarshalJSON(j []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler and accepts numbers, strings, and null. JSON null or "" will set the rate to zero. If a string ends with '%' then the parsed number part will be divided by 100.

func (Rate) Valid

func (r Rate) Valid() bool

Valid returns if a is not infinite or NaN

func (Rate) ValidAndGreaterZero

func (r Rate) ValidAndGreaterZero() bool

func (Rate) ValidAndHasSign

func (r Rate) ValidAndHasSign(sign int) bool

ValidAndHasSign returns if r.Valid() and if it has the same sign than the passed int argument or any sign if 0 is passed.

func (Rate) ValidAndPositive

func (r Rate) ValidAndPositive() bool

func (Rate) ValidAndSmallerZero

func (r Rate) ValidAndSmallerZero() bool

func (Rate) WithNegSign

func (r Rate) WithNegSign(negative bool) Rate

WithNegSign returns the value with a negative sign if true is passed, or with a positive sign (abs) if false is passed.

func (Rate) WithPosSign

func (r Rate) WithPosSign(positive bool) Rate

WithPosSign returns the value with a positive sign (abs) if true is passed, or with a negative sign if false is passed.

Jump to

Keyboard shortcuts

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