|
|
@@ -496,7 +496,202 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtCurrency(num float64, v uint64, curren
|
|
|
|
|
|
return b
|
|
|
{{ else }}
|
|
|
- return []byte(s)
|
|
|
+ return append(append([]byte{}, symbol...), s...)
|
|
|
+ {{ end -}}
|
|
|
+}
|
|
|
+
|
|
|
+// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
|
|
|
+// in accounting notation. returned as a []byte just in case the caller wishes to add more and can help
|
|
|
+// avoid allocations; otherwise just cast as string.
|
|
|
+func({{ .BaseLocale }} *{{ .Locale }}) FmtAccounting(num float64, v uint64, currency currency.Type) []byte {
|
|
|
+
|
|
|
+ s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
|
|
|
+ symbol := {{ .BaseLocale }}.currencies[currency]
|
|
|
+ {{- if eq .FmtCurrencyExists true }}
|
|
|
+ {{- if gt .FmtCurrencyGroupLen 0 }}
|
|
|
+ l := len(s) + len({{ .BaseLocale }}.decimal) + len({{ .BaseLocale }}.group) * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
|
|
|
+ count := 0
|
|
|
+ inWhole := v == 0
|
|
|
+ {{- if gt .FmtCurrencySecondaryGroupLen 0}}
|
|
|
+ inSecondary := false
|
|
|
+ groupThreshold := {{ .FmtCurrencyGroupLen }}
|
|
|
+ {{ end -}}
|
|
|
+ {{ else }}
|
|
|
+ l := len(s) + len({{ .BaseLocale }}.decimal)
|
|
|
+ {{ end }}
|
|
|
+ b := make([]byte, 0, l)
|
|
|
+
|
|
|
+ for i := len(s) - 1; i >= 0; i-- {
|
|
|
+
|
|
|
+ if s[i] == '.' {
|
|
|
+
|
|
|
+ {{- if eq .DecimalLen 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.decimal[0])
|
|
|
+ {{- else }}
|
|
|
+ for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.decimal[j])
|
|
|
+ }
|
|
|
+ {{ end -}}
|
|
|
+ {{- if gt .FmtCurrencyGroupLen 0 }}
|
|
|
+ inWhole = true
|
|
|
+ {{ end }}
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ {{ if gt .FmtCurrencyGroupLen 0 }}
|
|
|
+ if inWhole {
|
|
|
+
|
|
|
+ {{- if gt .FmtCurrencySecondaryGroupLen 0}}
|
|
|
+
|
|
|
+ if count == groupThreshold {
|
|
|
+ {{- if eq .GroupLen 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.group[0])
|
|
|
+ {{- else }}
|
|
|
+ for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.group[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+ count = 1
|
|
|
+
|
|
|
+ if !inSecondary {
|
|
|
+ inSecondary = true
|
|
|
+ groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
|
|
|
+ }
|
|
|
+ {{ else }}
|
|
|
+ if count == {{ .FmtCurrencyGroupLen }} {
|
|
|
+ {{- if eq .GroupLen 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.group[0])
|
|
|
+ {{- else }}
|
|
|
+ for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.group[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+ count = 1
|
|
|
+ {{ end -}}
|
|
|
+ } else {
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ b = append(b, s[i])
|
|
|
+ }
|
|
|
+
|
|
|
+ if num < 0 {
|
|
|
+
|
|
|
+ {{ if and .FmtCurrencyNegativeInPrefix (not .FmtCurrencyNegativeLeft) }}
|
|
|
+ for j := len(symbol) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, symbol[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if gt (len .FmtCurrencyNegativePrefix) 0}}
|
|
|
+ {{- if eq (len .FmtCurrencyNegativePrefix) 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[0])
|
|
|
+ {{ else }}
|
|
|
+ for j := len({{ .BaseLocale }}.currencyNegativePrefix) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if and .FmtCurrencyNegativeInPrefix .FmtCurrencyNegativeLeft }}
|
|
|
+ for j := len(symbol) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, symbol[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if eq (not .FmtCurrencyNegativeExists) true}}
|
|
|
+ {{- if eq .MinusLen 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.minus[0])
|
|
|
+ {{ else }}
|
|
|
+ for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.minus[j])
|
|
|
+ }
|
|
|
+ {{ end -}}
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if or .FmtCurrencyInPrefix (gt (len .FmtCurrencyPrefix) 0) }}
|
|
|
+ } else {
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
|
|
|
+ for j := len(symbol) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, symbol[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if gt (len .FmtCurrencyPrefix) 0}}
|
|
|
+ {{- if eq (len .FmtCurrencyPrefix) 1 }}
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
|
|
|
+ {{ else }}
|
|
|
+ for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
|
|
|
+ for j := len(symbol) - 1; j >= 0; j-- {
|
|
|
+ b = append(b, symbol[j])
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+ }
|
|
|
+
|
|
|
+ // reverse
|
|
|
+ for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
|
|
|
+ b[i], b[j] = b[j], b[i]
|
|
|
+ }
|
|
|
+
|
|
|
+ {{ if gt .FmtCurrencyMinDecimalLen 0 }}
|
|
|
+ if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
|
|
|
+
|
|
|
+ if v == 0 {
|
|
|
+ b = append(b, {{ .BaseLocale }}.decimal...)
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
|
|
|
+ b = append(b, '0')
|
|
|
+ }
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
|
|
|
+ if num < 0 {
|
|
|
+ {{ end }}
|
|
|
+ {{ if and (not .FmtCurrencyNegativeInPrefix) .FmtCurrencyNegativeLeft }}
|
|
|
+ b = append(b, symbol...)
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if gt (len .FmtCurrencyNegativeSuffix) 0}}
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyNegativeSuffix...)
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if and (not .FmtCurrencyNegativeInPrefix) (not .FmtCurrencyNegativeLeft) }}
|
|
|
+ b = append(b, symbol...)
|
|
|
+ {{ end }}
|
|
|
+ {{ if or (not .FmtCurrencyInPrefix) (gt (len .FmtCurrencySuffix) 0)}}
|
|
|
+ } else {
|
|
|
+ {{ end }}
|
|
|
+ {{ if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
|
|
|
+ b = append(b, symbol...)
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if gt (len .FmtCurrencySuffix) 0}}
|
|
|
+ b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ {{ if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
|
|
|
+ b = append(b, symbol...)
|
|
|
+ {{ end }}
|
|
|
+ {{ if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
|
|
|
+ }
|
|
|
+ {{ end }}
|
|
|
+
|
|
|
+ return b
|
|
|
+ {{ else }}
|
|
|
+ return append(append([]byte{}, symbol...), s...)
|
|
|
{{ end -}}
|
|
|
}
|
|
|
|