-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathamount.go
More file actions
31 lines (24 loc) · 797 Bytes
/
amount.go
File metadata and controls
31 lines (24 loc) · 797 Bytes
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
package money
import "fmt"
// Amount represents money in minor units (e.g., kuruş).
// 12.34 TL => 1234
type Amount int64
func NewMinor(minor int64) Amount { return Amount(minor) }
func (a Amount) Minor() int64 { return int64(a) }
func (a Amount) Add(b Amount) Amount { return a + b }
func (a Amount) Sub(b Amount) Amount { return a - b }
// MulQty multiplies by quantity (e.g., unit price * qty).
func (a Amount) MulQty(qty int64) Amount { return Amount(int64(a) * qty) }
func (a Amount) IsNegative() bool { return a < 0 }
// StringFixed2 formats as "12.34" (always 2 decimals).
func (a Amount) StringFixed2() string {
min := int64(a)
sign := ""
if min < 0 {
sign = "-"
min = -min
}
whole := min / 100
frac := min % 100
return fmt.Sprintf("%s%d.%02d", sign, whole, frac)
}