mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
remove 15 interval RoundCash (#166)
* remove 15 interval RoundCash * update function doc * Remove comments and benchmark referring interval 15 Co-authored-by: Mateusz Woś <wos.mateusz16@gmail.com>
This commit is contained in:
parent
408a2507e1
commit
6c07c6ca86
3 changed files with 3 additions and 36 deletions
19
decimal.go
19
decimal.go
|
@ -844,10 +844,9 @@ func (d Decimal) RoundBank(places int32) Decimal {
|
||||||
// RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific
|
// RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific
|
||||||
// interval. The amount payable for a cash transaction is rounded to the nearest
|
// interval. The amount payable for a cash transaction is rounded to the nearest
|
||||||
// multiple of the minimum currency unit available. The following intervals are
|
// 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
|
// 5: 5 cent rounding 3.43 => 3.45
|
||||||
// 10: 10 cent rounding 3.45 => 3.50 (5 gets rounded up)
|
// 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
|
// 25: 25 cent rounding 3.41 => 3.50
|
||||||
// 50: 50 cent rounding 3.75 => 4.00
|
// 50: 50 cent rounding 3.75 => 4.00
|
||||||
// 100: 100 cent rounding 3.50 => 4.00
|
// 100: 100 cent rounding 3.50 => 4.00
|
||||||
|
@ -859,20 +858,6 @@ func (d Decimal) RoundCash(interval uint8) Decimal {
|
||||||
iVal = twentyInt
|
iVal = twentyInt
|
||||||
case 10:
|
case 10:
|
||||||
iVal = tenInt
|
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:
|
case 25:
|
||||||
iVal = fourInt
|
iVal = fourInt
|
||||||
case 50:
|
case 50:
|
||||||
|
@ -880,7 +865,7 @@ func (d Decimal) RoundCash(interval uint8) Decimal {
|
||||||
case 100:
|
case 100:
|
||||||
iVal = oneInt
|
iVal = oneInt
|
||||||
default:
|
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{
|
dVal := Decimal{
|
||||||
value: iVal,
|
value: iVal,
|
||||||
|
|
|
@ -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) {
|
func Benchmark_Cmp(b *testing.B) {
|
||||||
decimals := DecimalSlice([]Decimal{})
|
decimals := DecimalSlice([]Decimal{})
|
||||||
for i := 0; i < 1000000; i++ {
|
for i := 0; i < 1000000; i++ {
|
||||||
|
|
|
@ -1565,14 +1565,6 @@ func TestDecimal_RoundCash(t *testing.T) {
|
||||||
{"3.95", 10, "4.00"},
|
{"3.95", 10, "4.00"},
|
||||||
{"395", 10, "395"},
|
{"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.23", 25, "3.25"},
|
||||||
{"3.33", 25, "3.25"},
|
{"3.33", 25, "3.25"},
|
||||||
{"3.53", 25, "3.50"},
|
{"3.53", 25, "3.50"},
|
||||||
|
@ -1608,7 +1600,7 @@ func TestDecimal_RoundCash_Panic(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
if have, ok := r.(string); ok {
|
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 {
|
if want != have {
|
||||||
t.Errorf("\nWant: %q\nHave: %q", want, have)
|
t.Errorf("\nWant: %q\nHave: %q", want, have)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue