1
1
// Package decimal implements an arbitrary precision fixed-point decimal.
2
2
//
3
- // To use as part of a struct:
4
- //
5
- // type Struct struct {
6
- // Number Decimal
7
- // }
8
- //
9
3
// The zero-value of a Decimal is 0, as you would expect.
10
4
//
11
5
// The best way to create a new Decimal is to use decimal.NewFromString, ex:
12
6
//
13
7
// n, err := decimal.NewFromString("-123.4567")
14
8
// n.String() // output: "-123.4567"
15
9
//
16
- // NOTE: This can "only" represent numbers with a maximum of 2^31 digits
17
- // after the decimal point.
10
+ // To use Decimal as part of a struct:
11
+ //
12
+ // type Struct struct {
13
+ // Number Decimal
14
+ // }
15
+ //
16
+ // Note: This can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
18
17
package decimal
19
18
20
19
import (
@@ -32,14 +31,14 @@ import (
32
31
//
33
32
// Example:
34
33
//
35
- // d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
34
+ // d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
36
35
// d1.String() // output: "0.6666666666666667"
37
- // d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000)
36
+ // d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000))
38
37
// d2.String() // output: "0.0000666666666667"
39
- // d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3)
38
+ // d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3))
40
39
// d3.String() // output: "6666.6666666666666667"
41
40
// decimal.DivisionPrecision = 3
42
- // d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
41
+ // d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
43
42
// d4.String() // output: "0.667"
44
43
//
45
44
var DivisionPrecision = 16
@@ -53,6 +52,7 @@ var DivisionPrecision = 16
53
52
var MarshalJSONWithoutQuotes = false
54
53
55
54
// Zero constant, to make computations faster.
55
+ // Zero should never be compared with == or != directly, please use decimal.Equal or decimal.Cmp instead.
56
56
var Zero = New (0 , 1 )
57
57
58
58
var zeroInt = big .NewInt (0 )
@@ -119,11 +119,13 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal {
119
119
}
120
120
121
121
// NewFromString returns a new Decimal from a string representation.
122
+ // Trailing zeroes are not trimmed.
122
123
//
123
124
// Example:
124
125
//
125
126
// d, err := NewFromString("-123.45")
126
127
// d2, err := NewFromString(".0001")
128
+ // d3, err := NewFromString("1.47000")
127
129
//
128
130
func NewFromString (value string ) (Decimal , error ) {
129
131
originalInput := value
@@ -207,7 +209,7 @@ func NewFromFloat(value float64) Decimal {
207
209
return newFromFloat (value , math .Float64bits (value ), & float64info )
208
210
}
209
211
210
- // NewFromFloat converts a float32 to Decimal.
212
+ // NewFromFloat32 converts a float32 to Decimal.
211
213
//
212
214
// The converted number will contain the number of significant digits that can be
213
215
// represented in a float with reliable roundtrip.
@@ -767,13 +769,13 @@ func (d Decimal) StringFixed(places int32) string {
767
769
//
768
770
// Example:
769
771
//
770
- // NewFromFloat(0).StringFixed (2) // output: "0.00"
771
- // NewFromFloat(0).StringFixed (0) // output: "0"
772
- // NewFromFloat(5.45).StringFixed (0) // output: "5"
773
- // NewFromFloat(5.45).StringFixed (1) // output: "5.4"
774
- // NewFromFloat(5.45).StringFixed (2) // output: "5.45"
775
- // NewFromFloat(5.45).StringFixed (3) // output: "5.450"
776
- // NewFromFloat(545).StringFixed (-1) // output: "550 "
772
+ // NewFromFloat(0).StringFixedBank (2) // output: "0.00"
773
+ // NewFromFloat(0).StringFixedBank (0) // output: "0"
774
+ // NewFromFloat(5.45).StringFixedBank (0) // output: "5"
775
+ // NewFromFloat(5.45).StringFixedBank (1) // output: "5.4"
776
+ // NewFromFloat(5.45).StringFixedBank (2) // output: "5.45"
777
+ // NewFromFloat(5.45).StringFixedBank (3) // output: "5.450"
778
+ // NewFromFloat(545).StringFixedBank (-1) // output: "540 "
777
779
//
778
780
func (d Decimal ) StringFixedBank (places int32 ) string {
779
781
rounded := d .RoundBank (places )
@@ -1167,7 +1169,7 @@ func Avg(first Decimal, rest ...Decimal) Decimal {
1167
1169
return sum .Div (count )
1168
1170
}
1169
1171
1170
- // Rescale two decimals to common exponential value (minimal exp of both decimals)
1172
+ // RescalePair rescales two decimals to common exponential value (minimal exp of both decimals)
1171
1173
func RescalePair (d1 Decimal , d2 Decimal ) (Decimal , Decimal ) {
1172
1174
d1 .ensureInitialized ()
1173
1175
d2 .ensureInitialized ()
0 commit comments