aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChad Gilbert <chad@freakingawesome.net>2018-01-19 06:52:17 +0800
committerVictor Quinn <mail@victorquinn.com>2018-01-19 06:52:17 +0800
commita76a8bf60b90042d6a0bfdbc50d7a6dae67a8ee8 (patch)
tree1b7776e0961bb43e3430b4d8587e896e45468d1a
parent8dffa2b7c7022feee4e1f55180695e79ca4261d4 (diff)
downloaddexon-decimal-a76a8bf60b90042d6a0bfdbc50d7a6dae67a8ee8.tar.gz
dexon-decimal-a76a8bf60b90042d6a0bfdbc50d7a6dae67a8ee8.tar.zst
dexon-decimal-a76a8bf60b90042d6a0bfdbc50d7a6dae67a8ee8.zip
Fix broken RoundBank func in #75 (#76)
-rw-r--r--decimal.go3
-rw-r--r--decimal_test.go21
2 files changed, 23 insertions, 1 deletions
diff --git a/decimal.go b/decimal.go
index 1d9399c..5ab9f26 100644
--- a/decimal.go
+++ b/decimal.go
@@ -619,7 +619,8 @@ func (d Decimal) RoundBank(places int32) Decimal {
round := d.Round(places)
remainder := d.Sub(round).Abs()
- if remainder.value.Cmp(fiveInt) == 0 && round.value.Bit(0) != 0 {
+ half := New(5, -places-1)
+ if remainder.Cmp(half) == 0 && round.value.Bit(0) != 0 {
if round.value.Sign() < 0 {
round.value.Add(round.value, oneInt)
} else {
diff --git a/decimal_test.go b/decimal_test.go
index ecb24ba..7979218 100644
--- a/decimal_test.go
+++ b/decimal_test.go
@@ -2108,3 +2108,24 @@ func TestAvg(t *testing.T) {
t.Errorf("Failed to calculate average, expected %s got %s", NewFromFloat(4.5).String(), avg.String())
}
}
+
+func TestRoundBankAnomaly(t *testing.T) {
+ a := New(25, -1)
+ b := New(250, -2)
+
+ if !a.Equal(b) {
+ t.Errorf("Expected %s to equal %s", a, b)
+ }
+
+ expected := New(2, 0)
+
+ aRounded := a.RoundBank(0)
+ if !aRounded.Equal(expected) {
+ t.Errorf("Expected bank rounding %s to equal %s, but it was %s", a, expected, aRounded)
+ }
+
+ bRounded := b.RoundBank(0)
+ if !bRounded.Equal(expected) {
+ t.Errorf("Expected bank rounding %s to equal %s, but it was %s", b, expected, bRounded)
+ }
+}