Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
remove 15 interval RoundCash (shopspring#166)
Browse files Browse the repository at this point in the history
* remove 15 interval RoundCash

* update function doc

* Remove comments and benchmark referring interval 15

Co-authored-by: Mateusz Woś <wos.mateusz16@gmail.com>
  • Loading branch information
fairyhunter13 and mwoss committed Feb 26, 2020
1 parent aa68b8d commit 679837e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 36 deletions.
19 changes: 2 additions & 17 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,9 @@ func (d Decimal) RoundBank(places int32) Decimal {
// RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific
// interval. The amount payable for a cash transaction is rounded to the nearest
// multiple of the minimum currency unit available. The following intervals are
// available: 5, 10, 15, 25, 50 and 100; any other number throws a panic.
// available: 5, 10, 25, 50 and 100; any other number throws a panic.
// 5: 5 cent rounding 3.43 => 3.45
// 10: 10 cent rounding 3.45 => 3.50 (5 gets rounded up)
// 15: 10 cent rounding 3.45 => 3.40 (5 gets rounded down)
// 25: 25 cent rounding 3.41 => 3.50
// 50: 50 cent rounding 3.75 => 4.00
// 100: 100 cent rounding 3.50 => 4.00
Expand All @@ -859,28 +858,14 @@ func (d Decimal) RoundCash(interval uint8) Decimal {
iVal = twentyInt
case 10:
iVal = tenInt
case 15:
if d.exp < 0 {
// TODO: optimize and reduce allocations
orgExp := d.exp
dOne := New(10^-int64(orgExp), orgExp)
d2 := d
d2.exp = 0
if d2.Mod(fiveDec).Equal(Zero) {
d2.exp = orgExp
d2 = d2.Sub(dOne)
d = d2
}
}
iVal = tenInt
case 25:
iVal = fourInt
case 50:
iVal = twoInt
case 100:
iVal = oneInt
default:
panic(fmt.Sprintf("Decimal does not support this Cash rounding interval `%d`. Supported: 5, 10, 15, 25, 50, 100", interval))
panic(fmt.Sprintf("Decimal does not support this Cash rounding interval `%d`. Supported: 5, 10, 25, 50, 100", interval))
}
dVal := Decimal{
value: iVal,
Expand Down
10 changes: 0 additions & 10 deletions decimal_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ func BenchmarkDecimal_RoundCash_Five(b *testing.B) {
}
}

func BenchmarkDecimal_RoundCash_Fifteen(b *testing.B) {
const want = "6.30"
for i := 0; i < b.N; i++ {
val := New(635, -2)
if have := val.StringFixedCash(15); have != want {
b.Fatalf("\nHave: %q\nWant: %q", have, want)
}
}
}

func Benchmark_Cmp(b *testing.B) {
decimals := DecimalSlice([]Decimal{})
for i := 0; i < 1000000; i++ {
Expand Down
10 changes: 1 addition & 9 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1565,14 +1565,6 @@ func TestDecimal_RoundCash(t *testing.T) {
{"3.95", 10, "4.00"},
{"395", 10, "395"},

{"6.42", 15, "6.40"},
{"6.39", 15, "6.40"},
{"6.35", 15, "6.30"},
{"6.36", 15, "6.40"},
{"6.349", 15, "6.30"},
{"6.30", 15, "6.30"},
{"666", 15, "666"},

{"3.23", 25, "3.25"},
{"3.33", 25, "3.25"},
{"3.53", 25, "3.50"},
Expand Down Expand Up @@ -1608,7 +1600,7 @@ func TestDecimal_RoundCash_Panic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if have, ok := r.(string); ok {
const want = "Decimal does not support this Cash rounding interval `231`. Supported: 5, 10, 15, 25, 50, 100"
const want = "Decimal does not support this Cash rounding interval `231`. Supported: 5, 10, 25, 50, 100"
if want != have {
t.Errorf("\nWant: %q\nHave: %q", want, have)
}
Expand Down

0 comments on commit 679837e

Please sign in to comment.