-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.go
123 lines (101 loc) · 2.63 KB
/
price.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package goprice
import (
"encoding/json"
"fmt"
"github.com/leekchan/accounting"
"math"
"strconv"
"strings"
)
const (
DefaultRoundOnFloat = 0.6
DefaultRoundPlacesFloat = 2
)
type Price float64
func NewPrice(value float64) *Price {
price := Price(value)
return &price
}
func (a Price) MarshalJSON() ([]byte, error) {
str := a.RoundPriceStringFormatAtCheckout(DefaultRoundOnFloat, DefaultRoundPlacesFloat)
return json.Marshal(str)
}
func (a *Price) UnmarshalJSON(data []byte) error {
var stringValue string
var floatValue float64
err := json.Unmarshal(data, &stringValue)
if err != nil {
return err
}
// remove ',' from string value
stringValue = strings.Replace(stringValue, ",", "", -1)
floatValue, err = strconv.ParseFloat(stringValue, 64)
if err != nil {
return err
}
*a = Price(floatValue)
return nil
}
func (a *Price) Add(value *Price) {
newPrice := *a + *value
*a = newPrice
}
func (a *Price) Multiply(value *Price) {
newPrice := *a * *value
*a = newPrice
}
func (a *Price) Minus(value *Price) {
newPrice := *a - *value
*a = newPrice
}
func (a *Price) Copy() *Price {
newPrice := *a
return &newPrice
}
func (a Price) ToStringWithCurrencySymbol(currencySymbol string) string {
str := a.RoundPriceStringFormatAtCheckout(DefaultRoundOnFloat, 0)
return fmt.Sprintf("%s%s", currencySymbol, str)
}
func (a Price) ToString() string {
return a.RoundPriceStringFormatAtCheckout(DefaultRoundOnFloat, DefaultRoundPlacesFloat)
}
func (a Price) ToFloat64() float64 {
return float64(a)
}
func (a Price) ToAdyenString(adyenInt *int) string {
if adyenInt == nil {
temp := a.ToAdyenInt()
adyenInt = &temp
}
return fmt.Sprintf("%d", adyenInt)
}
func (a Price) ToAdyenInt() int {
amountMulti100 := float64(a) * 100
amount := a.round(amountMulti100, DefaultRoundOnFloat, DefaultRoundPlacesFloat)
adyenAmount := int(amount)
return adyenAmount
}
func (a Price) round(val, roundOn float64, precision int) float64 {
var round float64
pow := math.Pow(10, float64(precision))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}
func (a Price) RoundPriceStringFormatAtCheckout(roundOn float64, precision int) string {
return a.priceStringFormatAtCheckout(a.round(a.ToFloat64(), roundOn, precision), precision)
}
func (a Price) priceStringFormatAtCheckout(val float64, precision int) string {
ac := accounting.Accounting{Precision: precision}
price := ac.FormatMoney(val)
return a.beautyMoney(price)
}
// beautyMoney format 2.00 to 2
func (a Price) beautyMoney(price string) string {
return strings.Replace(price, ".00", "", 1)
}