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/cy/cy.go
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 /cy/cy.go
parentdf0d272ef2250baaad947ff65501398623365e61 (diff)
Add FmtNumber function + logic.
Diffstat (limited to 'cy/cy.go')
-rw-r--r--cy/cy.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cy/cy.go b/cy/cy.go
index 418a6b85..154e0dad 100644
--- a/cy/cy.go
+++ b/cy/cy.go
@@ -2,6 +2,7 @@ package cy
import (
"math"
+ "strconv"
"github.com/go-playground/locales"
)
@@ -137,3 +138,56 @@ func (cy *cy) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64)
return locales.PluralRuleOther
}
+
+// FmtNumber returns 'num' with digits/precision of 'v' for 'cy' 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 (cy *cy) FmtNumber(num float64, v uint64) []byte {
+
+ s := strconv.FormatFloat(num, 'f', int(v), 64)
+
+ l := len(s) + len(cy.decimal) + len(cy.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(cy.decimal) - 1; j >= 0; j-- {
+ b = append(b, cy.decimal[j])
+ }
+
+ inWhole = true
+
+ continue
+ }
+
+ if inWhole {
+
+ if count == 3 {
+
+ for j := len(cy.group) - 1; j >= 0; j-- {
+ b = append(b, cy.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
+
+}