-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
49 lines (37 loc) · 989 Bytes
/
decimal.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
package decimal
import (
"time"
"github.com/shopspring/decimal"
)
type Decimal decimal.Decimal
var (
zero = Decimal(decimal.Zero)
one = FromFloat(1)
percent = FromFloat(0.01)
secondNano = FromInt64(int64(time.Second))
_percent = decimal.Decimal(percent)
)
func Zero() Decimal { return zero }
func One() Decimal { return one }
func Percent() Decimal { return percent }
func SecondNano() Decimal { return secondNano }
func FromInt64(n int64) Decimal {
return Decimal(decimal.NewFromFloat(float64(n)))
}
func FromInt(n int) Decimal {
return Decimal(decimal.NewFromFloat(float64(n)))
}
func FromFloat(n float64) Decimal {
return Decimal(decimal.NewFromFloat(n))
}
func FromString(str string) (Decimal, error) {
dec, err := decimal.NewFromString(str)
return Decimal(dec), err
}
func (d Decimal) String() string {
return decimal.Decimal(d).String()
}
func (d Decimal) Float64() float64 {
f, _ := decimal.Decimal(d).Float64()
return f
}