This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounding.go
65 lines (55 loc) · 1.66 KB
/
rounding.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package money
import (
"golang.org/x/text/currency"
)
// RoundingMode defines the rounding Mode to apply
type RoundingMode string
const (
// Down rounds down to the previous increment
// e.g. decimal: 1.49 increment: 0.1 result: 1.4
RoundDown RoundingMode = "down"
// Up rounds up to the next increment
// e.g. decimal: 1.41 increment: 0.1 result: 1.5
RoundUp RoundingMode = "up"
// ToNearest rounds to the nearest increment
// e.g. decimal: 1.45 increment: 0.1 result: 1.5
RoundToNearest RoundingMode = "to_nearest"
)
// RoundingKind defines a rounding standard for currencies
type RoundingKind string
var (
// RoundingStandard defines standard rounding and formatting for currencies.
RoundingStandard RoundingKind = "standard"
// RoundingCash defines rounding and formatting standards for cash
// transactions.
RoundingCash RoundingKind = "cash"
// RoundingAccounting defines rounding and formatting standards for
// accounting.
RoundingAccounting RoundingKind = "accounting"
)
func (k RoundingKind) kind() currency.Kind {
switch k {
case RoundingStandard:
return currency.Standard
case RoundingCash:
return currency.Cash
case RoundingAccounting:
return currency.Accounting
}
return currency.Standard
}
// Round rounds the given amount from the given unit
func Round(x Decimal, unit Decimal, mode RoundingMode) Decimal {
prec := unit.Exponent() * -1
switch mode {
case RoundDown:
rounded := x.RoundDown(prec)
return rounded.Sub(rounded.Mod(unit)).Truncate(prec)
case RoundUp:
rounded := x.RoundUp(prec)
return rounded.Add(rounded.Mod(unit)).Truncate(prec)
case RoundToNearest:
return x.RoundNearest(unit).Truncate(prec)
}
return Decimal{}
}