diff options
author | joeybloggs <Dean.Karn@gmail.com> | 2016-08-16 04:59:49 +0300 |
---|---|---|
committer | joeybloggs <Dean.Karn@gmail.com> | 2016-08-16 04:59:49 +0300 |
commit | aaa28727a7fc0cc7add5d60b31abfe62b983bedd (patch) | |
tree | 032798ba24e7ab36f7532d5ae66df80805029538 /cmd | |
parent | 5e30685010ab9c6da8cc729a3e7eab0b1ae964ae (diff) |
add accounting currency formatting logic + function
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/generate_resources.go | 8 | ||||
-rw-r--r-- | cmd/translator.tmpl | 197 |
2 files changed, 199 insertions, 6 deletions
diff --git a/cmd/generate_resources.go b/cmd/generate_resources.go index 1e028a2a..b5ba050c 100644 --- a/cmd/generate_resources.go +++ b/cmd/generate_resources.go @@ -94,6 +94,7 @@ type translator struct { FmtCurrencySuffix string FmtCurrencyInPrefix bool FmtCurrencyLeft bool + FmtCurrencyNegativeExists bool FmtCurrencyNegativePrefix string FmtCurrencyNegativeSuffix string FmtCurrencyNegativeInPrefix bool @@ -167,11 +168,6 @@ func main() { fmt.Println("Writing Data:", trans.Locale) - if trans.Locale == "en" { - fmt.Println("\t", trans.CurrencyNumberFormat) - fmt.Println("\t", trans.NegativeCurrencyNumberFormat) - } - if err = os.MkdirAll(fmt.Sprintf(locDir, trans.Locale), 0777); err != nil { log.Fatal(err) } @@ -585,6 +581,8 @@ func parseCurrencyNumberFormat(trans *translator) { return } + trans.FmtCurrencyNegativeExists = true + for idx = 0; idx < len(trans.NegativeCurrencyNumberFormat); idx++ { if trans.NegativeCurrencyNumberFormat[idx] == '#' || trans.NegativeCurrencyNumberFormat[idx] == '0' { diff --git a/cmd/translator.tmpl b/cmd/translator.tmpl index f66a3ef4..b6bd0020 100644 --- a/cmd/translator.tmpl +++ b/cmd/translator.tmpl @@ -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 -}} } |