internal/number: separate scale type
Scale of number may differ from scale of
increment. This is rare, but separating
concerns simplifies code down the line
and prevents bugs.
Change-Id: Icc5d9afe984f54875967033ed673a7d5fe6dd1fa
Reviewed-on: https://go-review.googlesource.com/60370
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/internal/number/decimal.go b/internal/number/decimal.go
index 8d80a92..f15b257 100644
--- a/internal/number/decimal.go
+++ b/internal/number/decimal.go
@@ -377,7 +377,7 @@
}
// Simple case: decimal notation
if r.Increment > 0 {
- scale := int(r.MaxFractionDigits)
+ scale := int(r.IncrementScale)
mult := 1.0
if scale > len(scales) {
mult = math.Pow(10, float64(scale))
diff --git a/internal/number/decimal_test.go b/internal/number/decimal_test.go
index ecb695a..fb856c8 100644
--- a/internal/number/decimal_test.go
+++ b/internal/number/decimal_test.go
@@ -245,7 +245,7 @@
scale2.SetScale(2)
scale2away := RoundingContext{Mode: AwayFromZero}
scale2away.SetScale(2)
- inc0_05 := RoundingContext{Increment: 5}
+ inc0_05 := RoundingContext{Increment: 5, IncrementScale: 2}
inc0_05.SetScale(2)
inc50 := RoundingContext{Increment: 50}
prec3 := RoundingContext{}
diff --git a/internal/number/pattern.go b/internal/number/pattern.go
index 4c3e815..2764cd2 100644
--- a/internal/number/pattern.go
+++ b/internal/number/pattern.go
@@ -55,12 +55,14 @@
// It contains all information needed to determine the "visible digits" as
// required by the pluralization rules.
type RoundingContext struct {
- Increment uint32
// TODO: unify these two fields so that there is a more unambiguous meaning
// of how precision is handled.
MaxSignificantDigits int16 // -1 is unlimited
MaxFractionDigits int16 // -1 is unlimited
+ Increment uint32
+ IncrementScale uint8 // May differ from printed scale.
+
Mode RoundingMode
DigitShift uint8 // Number of decimals to shift. Used for % and ‰.
@@ -210,6 +212,9 @@
} else {
p.Affix = affix
}
+ if p.Increment == 0 {
+ p.IncrementScale = 0
+ }
return p.Pattern, nil
}
@@ -423,6 +428,7 @@
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
p.Increment = p.Increment*10 + uint32(r-'0')
+ p.IncrementScale++
p.MinFractionDigits++
p.MaxFractionDigits++
case '#':
diff --git a/internal/number/pattern_test.go b/internal/number/pattern_test.go
index af08379..3a0b2be 100644
--- a/internal/number/pattern_test.go
+++ b/internal/number/pattern_test.go
@@ -272,6 +272,7 @@
FormatWidth: 4,
RoundingContext: RoundingContext{
Increment: 105,
+ IncrementScale: 2,
MinIntegerDigits: 1,
MinFractionDigits: 2,
MaxFractionDigits: 2,
@@ -285,6 +286,7 @@
GroupingSize: [2]uint8{2, 0},
RoundingContext: RoundingContext{
Increment: 105,
+ IncrementScale: 0,
MinIntegerDigits: 3,
MinFractionDigits: 0,
MaxFractionDigits: 0,