Пример #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
}
Пример #2
0
// Resets the package-wide decimal place by currency.
func SetDecimalByCurrency(cur string) {
	c := currency.Get(cur)
	if c != nil {
		decimal := newDecimal(c.DecimalDigits)
		DPf = float64(decimal)
		DP = int64(decimal)
	}
	return
}