diff options
Diffstat (limited to 'smn/smn.go')
-rw-r--r-- | smn/smn.go | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -153,3 +153,71 @@ func (smn *smn) FmtCurrency(num float64, v uint64, currency currency.Type) []byt return b } + +// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'smn' +// 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 (smn *smn) FmtAccounting(num float64, v uint64, currency currency.Type) []byte { + + s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) + symbol := smn.currencies[currency] + l := len(s) + len(smn.decimal) + + b := make([]byte, 0, l) + + for i := len(s) - 1; i >= 0; i-- { + + if s[i] == '.' { + for j := len(smn.decimal) - 1; j >= 0; j-- { + b = append(b, smn.decimal[j]) + } + + continue + } + + b = append(b, s[i]) + } + + if num < 0 { + + for j := len(symbol) - 1; j >= 0; j-- { + b = append(b, symbol[j]) + } + + for j := len(smn.currencyNegativePrefix) - 1; j >= 0; j-- { + b = append(b, smn.currencyNegativePrefix[j]) + } + + for j := len(smn.minus) - 1; j >= 0; j-- { + b = append(b, smn.minus[j]) + } + + } else { + + for j := len(symbol) - 1; j >= 0; j-- { + b = append(b, symbol[j]) + } + + for j := len(smn.currencyPositivePrefix) - 1; j >= 0; j-- { + b = append(b, smn.currencyPositivePrefix[j]) + } + + } + + // 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 num < 0 { + + b = append(b, smn.currencyNegativeSuffix...) + + } else { + + b = append(b, smn.currencyPositiveSuffix...) + + } + + return b +} |