diff options
author | joeybloggs <Dean.Karn@gmail.com> | 2016-08-13 05:03:24 +0300 |
---|---|---|
committer | joeybloggs <Dean.Karn@gmail.com> | 2016-08-13 05:03:24 +0300 |
commit | 99657750cc781e41c9c1e3be6b6316c18fc430bd (patch) | |
tree | 3366a7d2a645600dbe8efbadc86874f94c1cedb5 /mr_IN | |
parent | 7639a439740dfc3cdda4ae188bf3baf7cc2ff7a3 (diff) |
ad variable grouping logic + cleanup generated code newlines
Diffstat (limited to 'mr_IN')
-rw-r--r-- | mr_IN/mr_IN.go | 28 | ||||
-rw-r--r-- | mr_IN/mr_IN_test.go | 32 |
2 files changed, 46 insertions, 14 deletions
diff --git a/mr_IN/mr_IN.go b/mr_IN/mr_IN.go index 187bffe0..0f87983f 100644 --- a/mr_IN/mr_IN.go +++ b/mr_IN/mr_IN.go @@ -99,23 +99,19 @@ func (mr *mr_IN) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint6 // avoid allocations; otherwise just cast as string. func (mr *mr_IN) FmtNumber(num float64, v uint64) []byte { - s := strconv.FormatFloat(num, 'f', int(v), 64) - + s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) l := len(s) + len(mr.decimal) + len(mr.group)*len(s[:len(s)-int(v)-1])/3 - count := 0 inWhole := v == 0 + inSecondary := false + groupThreshold := 3 b := make([]byte, 0, l) for i := len(s) - 1; i >= 0; i-- { if s[i] == '.' { - - for j := len(mr.decimal) - 1; j >= 0; j-- { - b = append(b, mr.decimal[j]) - } - + b = append(b, mr.decimal[0]) inWhole = true continue @@ -123,13 +119,14 @@ func (mr *mr_IN) FmtNumber(num float64, v uint64) []byte { if inWhole { - if count == 3 { + if count == groupThreshold { + b = append(b, mr.group[0]) + count = 1 - for j := len(mr.group) - 1; j >= 0; j-- { - b = append(b, mr.group[j]) + if !inSecondary { + inSecondary = true + groupThreshold = 2 } - - count = 1 } else { count++ } @@ -138,11 +135,14 @@ func (mr *mr_IN) FmtNumber(num float64, v uint64) []byte { b = append(b, s[i]) } + if num < 0 { + b = append(b, mr.minus[0]) + } + // 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 - } diff --git a/mr_IN/mr_IN_test.go b/mr_IN/mr_IN_test.go new file mode 100644 index 00000000..e9e802b9 --- /dev/null +++ b/mr_IN/mr_IN_test.go @@ -0,0 +1,32 @@ +package mr_IN + +import "testing" + +func TestGrouping(t *testing.T) { + + tests := []struct { + num float64 + v uint64 + expected string + }{ + { + num: 1123456.5643, + v: 2, + expected: "11,23,456.56", + }, + } + + trans := New() + + for _, tt := range tests { + s := string(trans.FmtNumber(tt.num, tt.v)) + if s != tt.expected { + t.Errorf("Expected '%s' Got '%s", tt.expected, s) + } + } + + // num := float64(1123456.5643) + + // s := trans.FmtNumber(num, 2) + // fmt.Println(string(s)) +} |