forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.go
48 lines (34 loc) · 932 Bytes
/
calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package money
import "github.com/shopspring/decimal"
type calculator struct{}
func (c *calculator) add(a, b Amount) Amount {
return a.Add(b)
}
func (c *calculator) subtract(a, b Amount) Amount {
return a.Sub(b)
}
func (c *calculator) multiply(a Amount, m Amount) Amount {
return a.Mul(m)
}
func (c *calculator) divide(a Amount, d Amount, precision int32) Amount {
return a.Div(d).Truncate(precision)
}
func (c *calculator) modulus(a Amount, b Amount, precision int32) Amount {
_, rem := a.QuoRem(b, precision)
return rem
}
func (c *calculator) allocate(a, r, s Amount, precision int32) Amount {
if a.IsZero() || s.IsZero() {
return decimal.NewFromInt(0)
}
return a.Mul(r).DivRound(s, precision)
}
func (c *calculator) absolute(a Amount) Amount {
return a.Abs()
}
func (c *calculator) negative(a Amount) Amount {
return a.Neg()
}
func (c *calculator) round(a Amount, e int32) Amount {
return a.Round(e)
}