Ejemplo n.º 1
0
// GetFormatter returns a configured formatter given the locale and currency
// uses defaults for certain fields if locale or currency are blank/not recognised
func GetFormatter(locString, currencyCode string) *MoneyFormatter {
	formatter := &MoneyFormatter{
		decimalSeparator: defaultSeparator,
		groupSeparator:   defaultGroupSeparator,
		groupSize:        defaultGroupSize,
		decimalDigits:    defaultDecimalDigits,
		positivePattern:  defaultPositivePattern,
		negativePattern:  defaultNegativePattern,
		currencySymbol:   currencyCode,
	}
	if curr := currency.Get(currencyCode); curr != nil {
		formatter.currencySymbol = curr.Symbol
		formatter.decimalDigits = curr.DecimalDigits
	}
	if loc := locale.Get(locString); loc != nil {
		formatter.decimalSeparator = loc.CurrencyDecimalSeparator
		formatter.groupSeparator = loc.CurrencyGroupSeparator
		if len(loc.CurrencyGroupSizes) >= 1 {
			formatter.groupSize = loc.CurrencyGroupSizes[0]
		}
		formatter.negativePattern = loc.CurrencyNegativePattern
		formatter.positivePattern = loc.CurrencyPositivePattern
	} else {
		formatter.currencySymbol = currencyCode // do this because default format pattern puts the currency code at the end.
	}
	return formatter
}
Ejemplo n.º 2
0
// Sets the currency of Money by locale.
func (m *Money) SetCurrencyByLocale(lce string) *Money {
	l := locale.Get(lce)
	if l != nil {
		m.C = l.CurrencyCode
	}

	return m
}
Ejemplo n.º 3
0
// Resets the package-wide decimal place by locale.
func SetDecimalByLocale(lce string) {
	l := locale.Get(lce)
	if l != nil {
		decimal := newDecimal(l.CurrencyDecimalDigits)
		DPf = float64(decimal)
		DP = int64(decimal)
	}
	return
}