forked from shopspring/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
131 lines (110 loc) · 3.87 KB
/
options.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
123
124
125
126
127
128
129
130
131
package decimal
import "unicode/utf8"
type Options struct {
// Zero constant, to make computations faster.
// Zero should never be compared with == or != directly, please use decimal.Equal or decimal.Cmp instead.
zero Decimal
// divisionPrecision is the number of decimal places in the result when it
// doesn't divide exactly.
//
// Example:
//
// d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
// d1.String() // output: "0.6666666666666667"
// d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000))
// d2.String() // output: "0.0000666666666667"
// d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3))
// d3.String() // output: "6666.6666666666666667"
// decimal.DivisionPrecision = 3
// d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
// d4.String() // output: "0.667"
divisionPrecision int32
// decimalSeparator denotes a character that is used for separation integer and decimal parts
// Affects both FromString() and String()
decimalSeparator rune
// precalculated values for FromString()
isDecimalSeparator func(value string, i int) bool
decimalSeparatorBytes int
// marshalJSONWithoutQuotes should be set to true if you want the decimal to
// be JSON marshaled as a number, instead of as a string.
// WARNING: this is dangerous for decimals with many digits, since many JSON
// unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754
// double-precision floating point numbers, which means you can potentially
// silently lose precision.
marshalJSONWithoutQuotes bool
// expMaxIterations specifies the maximum number of iterations needed to calculate
// precise natural exponent value using ExpHullAbrham method.
expMaxIterations int
parseUnicodeMinus bool
}
func (opt *Options) Zero() Decimal {
return opt.zero
}
func (opt *Options) GetDivisionPrecision() int32 {
return opt.divisionPrecision
}
func (opt *Options) SetDivisionPrecision(divisionPrecision int32) *Options {
res := *opt
res.divisionPrecision = divisionPrecision
res.zero = res.zero.SetOptions(opt)
return &res
}
func (opt *Options) GetDecimalSeparator() rune {
return opt.decimalSeparator
}
func (opt *Options) SetDecimalSeparator(decimalSeparator rune) *Options {
res := *opt
res.decimalSeparator = decimalSeparator
res.decimalSeparatorBytes = utf8.RuneLen(decimalSeparator)
if res.decimalSeparatorBytes > 1 {
strDecimalSeparator := string(decimalSeparator)
res.isDecimalSeparator = func(value string, i int) bool {
return value[i:i+res.decimalSeparatorBytes] == strDecimalSeparator
}
} else {
uint8DecimalSeparator := uint8(decimalSeparator)
res.isDecimalSeparator = func(value string, i int) bool {
return value[i] == uint8DecimalSeparator
}
}
res.zero = res.zero.SetOptions(opt)
return &res
}
func (opt *Options) GetMarshalJSONWithoutQuotes() bool {
return opt.marshalJSONWithoutQuotes
}
func (opt *Options) SetMarshalJSONWithoutQuotes(marshalJSONWithoutQuotes bool) *Options {
res := *opt
res.marshalJSONWithoutQuotes = marshalJSONWithoutQuotes
res.zero = res.zero.SetOptions(opt)
return &res
}
func (opt *Options) GetExpMaxIterations() int {
return opt.expMaxIterations
}
func (opt *Options) SetExpMaxIterations(expMaxIterations int) *Options {
res := *opt
res.expMaxIterations = expMaxIterations
res.zero = res.zero.SetOptions(opt)
return &res
}
func (opt *Options) GetParseUnicodeMinus() bool {
return opt.parseUnicodeMinus
}
func (opt *Options) SetParseUnicodeMinus(parseUnicodeMinus bool) *Options {
res := *opt
res.parseUnicodeMinus = parseUnicodeMinus
res.zero = res.zero.SetOptions(opt)
return &res
}
var DefaultOptions = &Options{
divisionPrecision: 16,
marshalJSONWithoutQuotes: false,
expMaxIterations: 1000,
parseUnicodeMinus: true,
}
func init() {
DefaultOptions = DefaultOptions.SetDecimalSeparator('.')
DefaultOptions.zero = DefaultOptions.New(0, 1)
Zero = DefaultOptions.zero
}