mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Compare commits
6 commits
d996c65fad
...
fee0dd4877
Author | SHA1 | Date | |
---|---|---|---|
|
fee0dd4877 | ||
|
a1bdfc355e | ||
|
275e48eaef | ||
|
dd603cbbbd | ||
|
afac94241f | ||
|
aaa9a02fd2 |
4 changed files with 92 additions and 81 deletions
27
CHANGELOG.md
27
CHANGELOG.md
|
@ -1,3 +1,30 @@
|
||||||
|
## Decimal v1.4.0
|
||||||
|
#### BREAKING
|
||||||
|
- Drop support for Go version older than 1.10 [#361](https://github.com/shopspring/decimal/pull/361)
|
||||||
|
|
||||||
|
#### FEATURES
|
||||||
|
- Add implementation of natural logarithm [#339](https://github.com/shopspring/decimal/pull/339) [#357](https://github.com/shopspring/decimal/pull/357)
|
||||||
|
- Add improved implementation of power operation [#358](https://github.com/shopspring/decimal/pull/358)
|
||||||
|
- Add Compare method which forwards calls to Cmp [#346](https://github.com/shopspring/decimal/pull/346)
|
||||||
|
- Add NewFromBigRat constructor [#288](https://github.com/shopspring/decimal/pull/288)
|
||||||
|
- Add NewFromUint64 constructor [#352](https://github.com/shopspring/decimal/pull/352)
|
||||||
|
|
||||||
|
#### ENHANCEMENTS
|
||||||
|
- Migrate to Github Actions [#245](https://github.com/shopspring/decimal/pull/245) [#340](https://github.com/shopspring/decimal/pull/340)
|
||||||
|
- Fix examples for RoundDown, RoundFloor, RoundUp, and RoundCeil [#285](https://github.com/shopspring/decimal/pull/285) [#328](https://github.com/shopspring/decimal/pull/328) [#341](https://github.com/shopspring/decimal/pull/341)
|
||||||
|
- Use Godoc standard to mark deprecated Equals and StringScaled methods [#342](https://github.com/shopspring/decimal/pull/342)
|
||||||
|
- Removed unnecessary min function for RescalePair method [#265](https://github.com/shopspring/decimal/pull/265)
|
||||||
|
- Avoid reallocation of initial slice in MarshalBinary (GobEncode) [#355](https://github.com/shopspring/decimal/pull/355)
|
||||||
|
- Optimize NumDigits method [#301](https://github.com/shopspring/decimal/pull/301) [#356](https://github.com/shopspring/decimal/pull/356)
|
||||||
|
- Optimize BigInt method [#359](https://github.com/shopspring/decimal/pull/359)
|
||||||
|
- Support scanning uint64 [#131](https://github.com/shopspring/decimal/pull/131) [#364](https://github.com/shopspring/decimal/pull/364)
|
||||||
|
- Add docs section with alternative libraries [#363](https://github.com/shopspring/decimal/pull/363)
|
||||||
|
|
||||||
|
#### BUGFIXES
|
||||||
|
- Fix incorrect calculation of decimal modulo [#258](https://github.com/shopspring/decimal/pull/258) [#317](https://github.com/shopspring/decimal/pull/317)
|
||||||
|
- Allocate new(big.Int) in Copy method to deeply clone it [#278](https://github.com/shopspring/decimal/pull/278)
|
||||||
|
- Fix overflow edge case in QuoRem method [#322](https://github.com/shopspring/decimal/pull/322)
|
||||||
|
|
||||||
## Decimal v1.3.1
|
## Decimal v1.3.1
|
||||||
|
|
||||||
#### ENHANCEMENTS
|
#### ENHANCEMENTS
|
||||||
|
|
16
README.md
16
README.md
|
@ -24,6 +24,11 @@ Run `go get github.com/shopspring/decimal`
|
||||||
|
|
||||||
Decimal library requires Go version `>=1.10`
|
Decimal library requires Go version `>=1.10`
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
http://godoc.org/github.com/shopspring/decimal
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -59,9 +64,16 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Documentation
|
## Alternative libraries
|
||||||
|
|
||||||
http://godoc.org/github.com/shopspring/decimal
|
When working with decimal numbers, you might face problems this library is not perfectly suited for.
|
||||||
|
Fortunately, thanks to the wonderful community we have a dozen other libraries that you can choose from.
|
||||||
|
Explore other alternatives to find the one that best fits your needs :)
|
||||||
|
|
||||||
|
* [cockroachdb/apd](https://github.com/cockroachdb/apd) - arbitrary precision, mutable and rich API similar to `big.Int`, more performant than this library
|
||||||
|
* [alpacahq/alpacadecimal](https://github.com/alpacahq/alpacadecimal) - high performance, low precision (12 digits), fully compatible API with this library
|
||||||
|
* [govalues/decimal](https://github.com/govalues/decimal) - high performance, zero-allocation, low precision (19 digits)
|
||||||
|
* [greatcloak/decimal](https://github.com/greatcloak/decimal) - fork focusing on billing and e-commerce web application related use cases, includes out-of-the-box BSON marshaling support
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
|
|
22
decimal.go
22
decimal.go
|
@ -9,7 +9,7 @@
|
||||||
//
|
//
|
||||||
// To use Decimal as part of a struct:
|
// To use Decimal as part of a struct:
|
||||||
//
|
//
|
||||||
// type StructName struct {
|
// type Struct struct {
|
||||||
// Number Decimal
|
// Number Decimal
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
@ -1351,6 +1351,13 @@ func (d Decimal) LessThanOrEqual(d2 Decimal) bool {
|
||||||
return cmp == -1 || cmp == 0
|
return cmp == -1 || cmp == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Betwee (LTE) return true when d is between d1 and d2 inclusive
|
||||||
|
func (d Decimal) IsBetween(d1 Decimal, d2 Decimal) bool {
|
||||||
|
cmp1 := d.Cmp(d1)
|
||||||
|
cmp2 := d.Cmp(d2)
|
||||||
|
return (cmp1 == 1 || cmp1 == 0) && (cmp2 == -1 || cmp2 == 0)
|
||||||
|
}
|
||||||
|
|
||||||
// Sign returns:
|
// Sign returns:
|
||||||
//
|
//
|
||||||
// -1 if d < 0
|
// -1 if d < 0
|
||||||
|
@ -1551,7 +1558,7 @@ func (d Decimal) Round(places int32) Decimal {
|
||||||
// NewFromFloat(545).RoundCeil(-2).String() // output: "600"
|
// NewFromFloat(545).RoundCeil(-2).String() // output: "600"
|
||||||
// NewFromFloat(500).RoundCeil(-2).String() // output: "500"
|
// NewFromFloat(500).RoundCeil(-2).String() // output: "500"
|
||||||
// NewFromFloat(1.1001).RoundCeil(2).String() // output: "1.11"
|
// NewFromFloat(1.1001).RoundCeil(2).String() // output: "1.11"
|
||||||
// NewFromFloat(-1.454).RoundCeil(1).String() // output: "-1.4"
|
// NewFromFloat(-1.454).RoundCeil(1).String() // output: "-1.5"
|
||||||
func (d Decimal) RoundCeil(places int32) Decimal {
|
func (d Decimal) RoundCeil(places int32) Decimal {
|
||||||
if d.exp >= -places {
|
if d.exp >= -places {
|
||||||
return d
|
return d
|
||||||
|
@ -1576,7 +1583,7 @@ func (d Decimal) RoundCeil(places int32) Decimal {
|
||||||
// NewFromFloat(545).RoundFloor(-2).String() // output: "500"
|
// NewFromFloat(545).RoundFloor(-2).String() // output: "500"
|
||||||
// NewFromFloat(-500).RoundFloor(-2).String() // output: "-500"
|
// NewFromFloat(-500).RoundFloor(-2).String() // output: "-500"
|
||||||
// NewFromFloat(1.1001).RoundFloor(2).String() // output: "1.1"
|
// NewFromFloat(1.1001).RoundFloor(2).String() // output: "1.1"
|
||||||
// NewFromFloat(-1.454).RoundFloor(1).String() // output: "-1.5"
|
// NewFromFloat(-1.454).RoundFloor(1).String() // output: "-1.4"
|
||||||
func (d Decimal) RoundFloor(places int32) Decimal {
|
func (d Decimal) RoundFloor(places int32) Decimal {
|
||||||
if d.exp >= -places {
|
if d.exp >= -places {
|
||||||
return d
|
return d
|
||||||
|
@ -1601,7 +1608,7 @@ func (d Decimal) RoundFloor(places int32) Decimal {
|
||||||
// NewFromFloat(545).RoundUp(-2).String() // output: "600"
|
// NewFromFloat(545).RoundUp(-2).String() // output: "600"
|
||||||
// NewFromFloat(500).RoundUp(-2).String() // output: "500"
|
// NewFromFloat(500).RoundUp(-2).String() // output: "500"
|
||||||
// NewFromFloat(1.1001).RoundUp(2).String() // output: "1.11"
|
// NewFromFloat(1.1001).RoundUp(2).String() // output: "1.11"
|
||||||
// NewFromFloat(-1.454).RoundUp(1).String() // output: "-1.5"
|
// NewFromFloat(-1.454).RoundUp(1).String() // output: "-1.4"
|
||||||
func (d Decimal) RoundUp(places int32) Decimal {
|
func (d Decimal) RoundUp(places int32) Decimal {
|
||||||
if d.exp >= -places {
|
if d.exp >= -places {
|
||||||
return d
|
return d
|
||||||
|
@ -1628,7 +1635,7 @@ func (d Decimal) RoundUp(places int32) Decimal {
|
||||||
// NewFromFloat(545).RoundDown(-2).String() // output: "500"
|
// NewFromFloat(545).RoundDown(-2).String() // output: "500"
|
||||||
// NewFromFloat(-500).RoundDown(-2).String() // output: "-500"
|
// NewFromFloat(-500).RoundDown(-2).String() // output: "-500"
|
||||||
// NewFromFloat(1.1001).RoundDown(2).String() // output: "1.1"
|
// NewFromFloat(1.1001).RoundDown(2).String() // output: "1.1"
|
||||||
// NewFromFloat(-1.454).RoundDown(1).String() // output: "-1.4"
|
// NewFromFloat(-1.454).RoundDown(1).String() // output: "-1.5"
|
||||||
func (d Decimal) RoundDown(places int32) Decimal {
|
func (d Decimal) RoundDown(places int32) Decimal {
|
||||||
if d.exp >= -places {
|
if d.exp >= -places {
|
||||||
return d
|
return d
|
||||||
|
@ -1847,6 +1854,11 @@ func (d *Decimal) Scan(value interface{}) error {
|
||||||
*d = New(v, 0)
|
*d = New(v, 0)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
case uint64:
|
||||||
|
// while clickhouse may send 0 in db as uint64
|
||||||
|
*d = NewFromUint64(v)
|
||||||
|
return nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// default is trying to interpret value stored as string
|
// default is trying to interpret value stored as string
|
||||||
str, err := unquoteIfQuoted(v)
|
str, err := unquoteIfQuoted(v)
|
||||||
|
|
104
decimal_test.go
104
decimal_test.go
|
@ -2416,104 +2416,57 @@ func TestDecimal_Max(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecimal_Scan(t *testing.T) {
|
func scanHelper(t *testing.T, dbval interface{}, expected Decimal) {
|
||||||
// test the Scan method that implements the
|
t.Helper()
|
||||||
// sql.Scanner interface
|
|
||||||
// check for the for different type of values
|
|
||||||
// that are possible to be received from the database
|
|
||||||
// drivers
|
|
||||||
|
|
||||||
// in normal operations the db driver (sqlite at least)
|
|
||||||
// will return an int64 if you specified a numeric format
|
|
||||||
a := Decimal{}
|
a := Decimal{}
|
||||||
dbvalue := 54.33
|
if err := a.Scan(dbval); err != nil {
|
||||||
expected := NewFromFloat(dbvalue)
|
|
||||||
|
|
||||||
err := a.Scan(dbvalue)
|
|
||||||
if err != nil {
|
|
||||||
// Scan failed... no need to test result value
|
// Scan failed... no need to test result value
|
||||||
t.Errorf("a.Scan(54.33) failed with message: %s", err)
|
t.Errorf("a.Scan(%v) failed with message: %s", dbval, err)
|
||||||
|
} else if !a.Equal(expected) {
|
||||||
} else {
|
|
||||||
// Scan succeeded... test resulting values
|
// Scan succeeded... test resulting values
|
||||||
if !a.Equal(expected) {
|
|
||||||
t.Errorf("%s does not equal to %s", a, expected)
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecimal_Scan(t *testing.T) {
|
||||||
|
// test the Scan method that implements the sql.Scanner interface
|
||||||
|
// check different types received from various database drivers
|
||||||
|
|
||||||
|
dbvalue := 54.33
|
||||||
|
expected := NewFromFloat(dbvalue)
|
||||||
|
scanHelper(t, dbvalue, expected)
|
||||||
|
|
||||||
// apparently MySQL 5.7.16 and returns these as float32 so we need
|
// apparently MySQL 5.7.16 and returns these as float32 so we need
|
||||||
// to handle these as well
|
// to handle these as well
|
||||||
dbvalueFloat32 := float32(54.33)
|
dbvalueFloat32 := float32(54.33)
|
||||||
expected = NewFromFloat(float64(dbvalueFloat32))
|
expected = NewFromFloat(float64(dbvalueFloat32))
|
||||||
|
scanHelper(t, dbvalueFloat32, expected)
|
||||||
err = a.Scan(dbvalueFloat32)
|
|
||||||
if err != nil {
|
|
||||||
// Scan failed... no need to test result value
|
|
||||||
t.Errorf("a.Scan(54.33) failed with message: %s", err)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Scan succeeded... test resulting values
|
|
||||||
if !a.Equal(expected) {
|
|
||||||
t.Errorf("%s does not equal to %s", a, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// at least SQLite returns an int64 when 0 is stored in the db
|
// at least SQLite returns an int64 when 0 is stored in the db
|
||||||
// and you specified a numeric format on the schema
|
// and you specified a numeric format on the schema
|
||||||
dbvalueInt := int64(0)
|
dbvalueInt := int64(0)
|
||||||
expected = New(dbvalueInt, 0)
|
expected = New(dbvalueInt, 0)
|
||||||
|
scanHelper(t, dbvalueInt, expected)
|
||||||
|
|
||||||
err = a.Scan(dbvalueInt)
|
// also test uint64
|
||||||
if err != nil {
|
dbvalueUint64 := uint64(2)
|
||||||
// Scan failed... no need to test result value
|
expected = New(2, 0)
|
||||||
t.Errorf("a.Scan(0) failed with message: %s", err)
|
scanHelper(t, dbvalueUint64, expected)
|
||||||
|
|
||||||
} else {
|
|
||||||
// Scan succeeded... test resulting values
|
|
||||||
if !a.Equal(expected) {
|
|
||||||
t.Errorf("%s does not equal to %s", a, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// in case you specified a varchar in your SQL schema,
|
// in case you specified a varchar in your SQL schema,
|
||||||
// the database driver will return byte slice []byte
|
// the database driver may return either []byte or string
|
||||||
valueStr := "535.666"
|
valueStr := "535.666"
|
||||||
dbvalueStr := []byte(valueStr)
|
dbvalueStr := []byte(valueStr)
|
||||||
expected, err = NewFromString(valueStr)
|
expected, err := NewFromString(valueStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
scanHelper(t, dbvalueStr, expected)
|
||||||
err = a.Scan(dbvalueStr)
|
scanHelper(t, valueStr, expected)
|
||||||
if err != nil {
|
|
||||||
// Scan failed... no need to test result value
|
|
||||||
t.Errorf("a.Scan('535.666') failed with message: %s", err)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Scan succeeded... test resulting values
|
|
||||||
if !a.Equal(expected) {
|
|
||||||
t.Errorf("%s does not equal to %s", a, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lib/pq can also return strings
|
|
||||||
expected, err = NewFromString(valueStr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = a.Scan(valueStr)
|
|
||||||
if err != nil {
|
|
||||||
// Scan failed... no need to test result value
|
|
||||||
t.Errorf("a.Scan('535.666') failed with message: %s", err)
|
|
||||||
} else {
|
|
||||||
// Scan succeeded... test resulting values
|
|
||||||
if !a.Equal(expected) {
|
|
||||||
t.Errorf("%s does not equal to %s", a, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type foo struct{}
|
type foo struct{}
|
||||||
|
a := Decimal{}
|
||||||
err = a.Scan(foo{})
|
err = a.Scan(foo{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("a.Scan(Foo{}) should have thrown an error but did not")
|
t.Errorf("a.Scan(Foo{}) should have thrown an error but did not")
|
||||||
|
@ -2614,6 +2567,13 @@ func TestDecimal_Equalities(t *testing.T) {
|
||||||
if c.LessThanOrEqual(b) {
|
if c.LessThanOrEqual(b) {
|
||||||
t.Errorf("%q should not be less than or equal %q", a, b)
|
t.Errorf("%q should not be less than or equal %q", a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !b.IsBetween(c, a) {
|
||||||
|
t.Errorf("%q should be less than or equal %q", a, b)
|
||||||
|
}
|
||||||
|
if b.IsBetween(a, c) {
|
||||||
|
t.Errorf("%q should not be less than or equal %q", a, b)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecimal_ScalesNotEqual(t *testing.T) {
|
func TestDecimal_ScalesNotEqual(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue