Skip to content

Commit 480f653

Browse files
authored
Update documentation, clean up package level documentation (shopspring#173)
* Update documentation, clean up package level documentation
1 parent c939845 commit 480f653

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
Arbitrary-precision fixed-point decimal numbers in go.
66

7-
NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
7+
_Note:_ Decimal library can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
88

99
## Features
1010

11-
* the zero-value is 0, and is safe to use without initialization
12-
* addition, subtraction, multiplication with no loss of precision
13-
* division with specified precision
14-
* database/sql serialization/deserialization
15-
* json and xml serialization/deserialization
11+
* The zero-value is 0, and is safe to use without initialization
12+
* Addition, subtraction, multiplication with no loss of precision
13+
* Division with specified precision
14+
* Database/sql serialization/deserialization
15+
* JSON and XML serialization/deserialization
1616

1717
## Install
1818

@@ -70,8 +70,8 @@ http://godoc.org/github.com/shopspring/decimal
7070

7171
#### Why don't you just use float64?
7272

73-
Because float64s (or any binary floating point type, actually) can't represent
74-
numbers such as 0.1 exactly.
73+
Because float64 (or any binary floating point type, actually) can't represent
74+
numbers such as `0.1` exactly.
7575

7676
Consider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that
7777
it prints out `10`, but it actually prints `9.999999999999831`. Over time,

decimal-go.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Can do binary floating point in multiprecision decimal precisely
99
// because 2 divides 10; cannot do decimal floating point
1010
// in multiprecision binary precisely.
11+
1112
package decimal
1213

1314
type decimal struct {

decimal.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// Package decimal implements an arbitrary precision fixed-point decimal.
22
//
3-
// To use as part of a struct:
4-
//
5-
// type Struct struct {
6-
// Number Decimal
7-
// }
8-
//
93
// The zero-value of a Decimal is 0, as you would expect.
104
//
115
// The best way to create a new Decimal is to use decimal.NewFromString, ex:
126
//
137
// n, err := decimal.NewFromString("-123.4567")
148
// n.String() // output: "-123.4567"
159
//
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.
1817
package decimal
1918

2019
import (
@@ -32,14 +31,14 @@ import (
3231
//
3332
// Example:
3433
//
35-
// d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
34+
// d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
3635
// d1.String() // output: "0.6666666666666667"
37-
// d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000)
36+
// d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000))
3837
// d2.String() // output: "0.0000666666666667"
39-
// d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3)
38+
// d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3))
4039
// d3.String() // output: "6666.6666666666666667"
4140
// decimal.DivisionPrecision = 3
42-
// d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
41+
// d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
4342
// d4.String() // output: "0.667"
4443
//
4544
var DivisionPrecision = 16
@@ -53,6 +52,7 @@ var DivisionPrecision = 16
5352
var MarshalJSONWithoutQuotes = false
5453

5554
// Zero constant, to make computations faster.
55+
// Zero should never be compared with == or != directly, please use decimal.Equal or decimal.Cmp instead.
5656
var Zero = New(0, 1)
5757

5858
var zeroInt = big.NewInt(0)
@@ -119,11 +119,13 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal {
119119
}
120120

121121
// NewFromString returns a new Decimal from a string representation.
122+
// Trailing zeroes are not trimmed.
122123
//
123124
// Example:
124125
//
125126
// d, err := NewFromString("-123.45")
126127
// d2, err := NewFromString(".0001")
128+
// d3, err := NewFromString("1.47000")
127129
//
128130
func NewFromString(value string) (Decimal, error) {
129131
originalInput := value
@@ -207,7 +209,7 @@ func NewFromFloat(value float64) Decimal {
207209
return newFromFloat(value, math.Float64bits(value), &float64info)
208210
}
209211

210-
// NewFromFloat converts a float32 to Decimal.
212+
// NewFromFloat32 converts a float32 to Decimal.
211213
//
212214
// The converted number will contain the number of significant digits that can be
213215
// represented in a float with reliable roundtrip.
@@ -767,13 +769,13 @@ func (d Decimal) StringFixed(places int32) string {
767769
//
768770
// Example:
769771
//
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"
777779
//
778780
func (d Decimal) StringFixedBank(places int32) string {
779781
rounded := d.RoundBank(places)
@@ -1167,7 +1169,7 @@ func Avg(first Decimal, rest ...Decimal) Decimal {
11671169
return sum.Div(count)
11681170
}
11691171

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)
11711173
func RescalePair(d1 Decimal, d2 Decimal) (Decimal, Decimal) {
11721174
d1.ensureInitialized()
11731175
d2.ensureInitialized()

rounding.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Can do binary floating point in multiprecision decimal precisely
99
// because 2 divides 10; cannot do decimal floating point
1010
// in multiprecision binary precisely.
11+
1112
package decimal
1213

1314
type floatInfo struct {

0 commit comments

Comments
 (0)