forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.go
43 lines (32 loc) · 831 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
package money
type calculator struct{}
func (c *calculator) add(a, b *Amount) *Amount {
return &Amount{a.val + b.val}
}
func (c *calculator) subtract(a, b *Amount) *Amount {
return &Amount{a.val - b.val}
}
func (c *calculator) multiply(a *Amount, m int64) *Amount {
return &Amount{a.val * m}
}
func (c *calculator) divide(a *Amount, d int64) *Amount {
return &Amount{a.val / d}
}
func (c *calculator) modulus(a *Amount, d int64) *Amount {
return &Amount{a.val % d}
}
func (c *calculator) allocate(a *Amount, r, s int) *Amount {
return &Amount{a.val * int64(r) / int64(s)}
}
func (c *calculator) absolute(a *Amount) *Amount {
if a.val < 0 {
return &Amount{-a.val}
}
return &Amount{a.val}
}
func (c *calculator) negative(a *Amount) *Amount {
if a.val > 0 {
return &Amount{-a.val}
}
return &Amount{a.val}
}