/
store_sum.go
72 lines (66 loc) · 1.45 KB
/
store_sum.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
package store
import (
"math/big"
"strconv"
"github.com/shopspring/decimal"
)
func (b *baseStore) SumBigInt(ord uint64, key string, value *big.Int) {
sum := new(big.Int)
val, found := b.GetAt(ord, key)
if !found {
sum = value
} else {
prev, _ := new(big.Int).SetString(string(val), 10)
if prev == nil {
sum = value
} else {
sum.Add(prev, value)
}
}
b.set(ord, key, []byte(sum.String()))
}
func (b *baseStore) SumInt64(ord uint64, key string, value int64) {
var sum int64
val, found := b.GetAt(ord, key)
if !found {
sum = value
} else {
prev, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
sum = value
} else {
sum = prev + value
}
}
b.set(ord, key, []byte(strconv.FormatInt(sum, 10)))
}
func (b *baseStore) SumFloat64(ord uint64, key string, value float64) {
var sum float64
val, found := b.GetAt(ord, key)
if !found {
sum = value
} else {
prev, err := strconv.ParseFloat(string(val), 64)
if err != nil {
sum = value
} else {
sum = prev + value
}
}
b.set(ord, key, []byte(strconv.FormatFloat(sum, 'g', 100, 64)))
}
func (b *baseStore) SumBigDecimal(ord uint64, key string, value decimal.Decimal) {
v, found := b.GetAt(ord, key)
if !found {
b.set(ord, key, []byte(value.String()))
return
}
prev, err := decimal.NewFromString(string(v))
prev.Truncate(34)
if err != nil {
b.set(ord, key, []byte(value.String()))
return
}
sum := prev.Add(value)
b.set(ord, key, []byte(sum.String()))
}