api-entities/common/vo/currency.go

43 lines
1.2 KiB
Go
Raw Normal View History

2024-12-06 16:14:44 +01:00
package vo
import "strings"
type Currency struct {
Code string
NumericCode string
Fraction int
Grapheme string
Template string
Decimal string
Thousand string
}
type Currencies map[string]*Currency
var currencies = Currencies{
"EUR": {Decimal: ".", Thousand: ",", Code: "EUR", Fraction: 2, NumericCode: "978", Grapheme: "\u20ac", Template: "$1"},
"GBP": {Decimal: ".", Thousand: ",", Code: "GBP", Fraction: 2, NumericCode: "826", Grapheme: "\u00a3", Template: "$1"},
"PLN": {Decimal: ".", Thousand: ",", Code: "PLN", Fraction: 2, NumericCode: "985", Grapheme: "z\u0142", Template: "1 $"},
"USD": {Decimal: ".", Thousand: ",", Code: "USD", Fraction: 2, NumericCode: "840", Grapheme: "$", Template: "$1"},
}
func NewCurrency(code string) *Currency {
return &Currency{Code: strings.ToUpper(code)}
}
func (c *Currency) Default() *Currency {
return &Currency{Decimal: ".", Thousand: ",", Code: c.Code, Fraction: 2, Grapheme: c.Code, Template: "1$"}
}
func (c *Currency) Get() *Currency {
if curr, ok := currencies[c.Code]; ok {
return curr
}
return c.getDefault()
}
func (c *Currency) Equals(oc *Currency) bool {
return c.Code == oc.Code
}