Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/locales.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/en
diff options
context:
space:
mode:
authorjoeybloggs <Dean.Karn@gmail.com>2016-08-12 19:15:42 +0300
committerjoeybloggs <Dean.Karn@gmail.com>2016-08-12 19:15:42 +0300
commit7639a439740dfc3cdda4ae188bf3baf7cc2ff7a3 (patch)
tree7c43c1081bb3e25696701bd8e13ab0efee92cec4 /en
parentdf0d272ef2250baaad947ff65501398623365e61 (diff)
Add FmtNumber function + logic.
Diffstat (limited to 'en')
-rw-r--r--en/en.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/en/en.go b/en/en.go
index c8785f2d..a0d0d15d 100644
--- a/en/en.go
+++ b/en/en.go
@@ -2,6 +2,7 @@ package en
import (
"math"
+ "strconv"
"github.com/go-playground/locales"
)
@@ -83,3 +84,56 @@ func (en *en) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
func (en *en) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
return locales.PluralRuleOther
}
+
+// FmtNumber returns 'num' with digits/precision of 'v' for 'en' and handles both Whole and Real numbers based on 'v'
+// returned as a []byte just in case the caller wishes to add more and can help
+// avoid allocations; otherwise just cast as string.
+func (en *en) FmtNumber(num float64, v uint64) []byte {
+
+ s := strconv.FormatFloat(num, 'f', int(v), 64)
+
+ l := len(s) + len(en.decimal) + len(en.group)*len(s[:len(s)-int(v)-1])/3
+
+ count := 0
+ inWhole := v == 0
+
+ b := make([]byte, 0, l)
+
+ for i := len(s) - 1; i >= 0; i-- {
+
+ if s[i] == '.' {
+
+ for j := len(en.decimal) - 1; j >= 0; j-- {
+ b = append(b, en.decimal[j])
+ }
+
+ inWhole = true
+
+ continue
+ }
+
+ if inWhole {
+
+ if count == 3 {
+
+ for j := len(en.group) - 1; j >= 0; j-- {
+ b = append(b, en.group[j])
+ }
+
+ count = 1
+ } else {
+ count++
+ }
+ }
+
+ b = append(b, s[i])
+ }
+
+ // reverse
+ for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
+ b[i], b[j] = b[j], b[i]
+ }
+
+ return b
+
+}