mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Compare commits
16 commits
059fdb5f61
...
661f71ebe2
Author | SHA1 | Date | |
---|---|---|---|
|
661f71ebe2 | ||
|
a1bdfc355e | ||
|
275e48eaef | ||
|
dd603cbbbd | ||
|
80ec940054 | ||
|
2fa107d0fe | ||
|
645a76e5b0 | ||
|
ed8f76e893 | ||
|
0e69d5cd53 | ||
|
bf7794e1a8 | ||
|
86c2d2acf2 | ||
|
519f778aec | ||
|
2371d4b37d | ||
|
54fa471190 | ||
|
bc0ec03c77 | ||
|
37706fc9ff |
9 changed files with 287 additions and 106 deletions
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -1,11 +1,15 @@
|
||||||
name: ci
|
name: ci
|
||||||
on: [push]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
jobs:
|
jobs:
|
||||||
ci-job:
|
ci-job:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go: [ '1.7.x', '1.18', '1.19', '1.20', '1.21', '1.x' ]
|
go: [ '1.10.x', '1.19', '1.20', '1.21', '1.22', '1.x' ]
|
||||||
name: Go ${{ matrix.go }}
|
name: Go ${{ matrix.go }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@
|
||||||
# IntelliJ
|
# IntelliJ
|
||||||
.idea/
|
.idea/
|
||||||
*.iml
|
*.iml
|
||||||
|
*.sum
|
||||||
|
|
||||||
# VS code
|
# VS code
|
||||||
*.code-workspace
|
*.code-workspace
|
||||||
|
|
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
|
||||||
|
|
21
README.md
21
README.md
|
@ -22,7 +22,12 @@ Run `go get github.com/shopspring/decimal`
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
Decimal library requires Go version `>=1.7`
|
Decimal library requires Go version `>=1.10`
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
http://godoc.org/github.com/shopspring/decimal
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -59,14 +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 :)
|
||||||
|
|
||||||
## Production Usage
|
* [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
|
||||||
* [Spring](https://shopspring.com/), since August 14, 2014.
|
* [govalues/decimal](https://github.com/govalues/decimal) - high performance, zero-allocation, low precision (19 digits)
|
||||||
* If you are using this in production, please let us know!
|
* [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
|
||||||
|
|
||||||
|
|
52
decimal.go
52
decimal.go
|
@ -130,6 +130,18 @@ func NewFromInt32(value int32) Decimal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromUint64 converts an uint64 to Decimal.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// NewFromUint64(123).String() // output: "123"
|
||||||
|
func NewFromUint64(value uint64) Decimal {
|
||||||
|
return Decimal{
|
||||||
|
value: new(big.Int).SetUint64(value),
|
||||||
|
exp: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
||||||
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
||||||
return Decimal{
|
return Decimal{
|
||||||
|
@ -1224,14 +1236,33 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumDigits returns the number of digits of the decimal coefficient (d.Value)
|
// NumDigits returns the number of digits of the decimal coefficient (d.Value)
|
||||||
// Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part
|
|
||||||
func (d Decimal) NumDigits() int {
|
func (d Decimal) NumDigits() int {
|
||||||
d.ensureInitialized()
|
if d.value == nil {
|
||||||
// Note(mwoss): It can be optimized, unnecessary cast of big.Int to string
|
return 1
|
||||||
if d.IsNegative() {
|
|
||||||
return len(d.value.String()) - 1
|
|
||||||
}
|
}
|
||||||
return len(d.value.String())
|
|
||||||
|
if d.value.IsInt64() {
|
||||||
|
i64 := d.value.Int64()
|
||||||
|
// restrict fast path to integers with exact conversion to float64
|
||||||
|
if i64 <= (1<<53) && i64 >= -(1<<53) {
|
||||||
|
if i64 == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return int(math.Log10(math.Abs(float64(i64)))) + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
estimatedNumDigits := int(float64(d.value.BitLen()) / math.Log2(10))
|
||||||
|
|
||||||
|
// estimatedNumDigits (lg10) may be off by 1, need to verify
|
||||||
|
digitsBigInt := big.NewInt(int64(estimatedNumDigits))
|
||||||
|
errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil)
|
||||||
|
|
||||||
|
if d.value.CmpAbs(errorCorrectionUnit) >= 0 {
|
||||||
|
return estimatedNumDigits + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return estimatedNumDigits
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.
|
// IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.
|
||||||
|
@ -1387,9 +1418,7 @@ func (d Decimal) IntPart() int64 {
|
||||||
// BigInt returns integer component of the decimal as a BigInt.
|
// BigInt returns integer component of the decimal as a BigInt.
|
||||||
func (d Decimal) BigInt() *big.Int {
|
func (d Decimal) BigInt() *big.Int {
|
||||||
scaledD := d.rescale(0)
|
scaledD := d.rescale(0)
|
||||||
i := &big.Int{}
|
return scaledD.value
|
||||||
i.SetString(scaledD.String(), 10)
|
|
||||||
return i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BigFloat returns decimal as BigFloat.
|
// BigFloat returns decimal as BigFloat.
|
||||||
|
@ -1818,6 +1847,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)
|
||||||
|
|
|
@ -121,6 +121,34 @@ func BenchmarkDecimal_RoundCash_Five(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func numDigits(b *testing.B, want int, val Decimal) {
|
||||||
|
b.Helper()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
if have := val.NumDigits(); have != want {
|
||||||
|
b.Fatalf("\nHave: %d\nWant: %d", have, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkDecimal_NumDigits10(b *testing.B) {
|
||||||
|
numDigits(b, 10, New(3478512345, -3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkDecimal_NumDigits100(b *testing.B) {
|
||||||
|
s := make([]byte, 102)
|
||||||
|
for i := range s {
|
||||||
|
s[i] = byte('0' + i%10)
|
||||||
|
}
|
||||||
|
s[0] = '-'
|
||||||
|
s[100] = '.'
|
||||||
|
d, err := NewFromString(string(s))
|
||||||
|
if err != nil {
|
||||||
|
b.Log(d)
|
||||||
|
b.Error(err)
|
||||||
|
}
|
||||||
|
numDigits(b, 100, d)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_Cmp(b *testing.B) {
|
func Benchmark_Cmp(b *testing.B) {
|
||||||
decimals := DecimalSlice([]Decimal{})
|
decimals := DecimalSlice([]Decimal{})
|
||||||
for i := 0; i < 1000000; i++ {
|
for i := 0; i < 1000000; i++ {
|
||||||
|
@ -132,7 +160,7 @@ func Benchmark_Cmp(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_decimal_Decimal_Add_different_precision(b *testing.B) {
|
func BenchmarkDecimal_Add_different_precision(b *testing.B) {
|
||||||
d1 := NewFromFloat(1000.123)
|
d1 := NewFromFloat(1000.123)
|
||||||
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
|
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
|
||||||
|
|
||||||
|
@ -143,7 +171,7 @@ func Benchmark_decimal_Decimal_Add_different_precision(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_decimal_Decimal_Sub_different_precision(b *testing.B) {
|
func BenchmarkDecimal_Sub_different_precision(b *testing.B) {
|
||||||
d1 := NewFromFloat(1000.123)
|
d1 := NewFromFloat(1000.123)
|
||||||
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
|
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
|
||||||
|
|
||||||
|
@ -154,7 +182,7 @@ func Benchmark_decimal_Decimal_Sub_different_precision(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_decimal_Decimal_Add_same_precision(b *testing.B) {
|
func BenchmarkDecimal_Add_same_precision(b *testing.B) {
|
||||||
d1 := NewFromFloat(1000.123)
|
d1 := NewFromFloat(1000.123)
|
||||||
d2 := NewFromFloat(500.123)
|
d2 := NewFromFloat(500.123)
|
||||||
|
|
||||||
|
@ -165,7 +193,7 @@ func Benchmark_decimal_Decimal_Add_same_precision(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_decimal_Decimal_Sub_same_precision(b *testing.B) {
|
func BenchmarkDecimal_Sub_same_precision(b *testing.B) {
|
||||||
d1 := NewFromFloat(1000.123)
|
d1 := NewFromFloat(1000.123)
|
||||||
d2 := NewFromFloat(500.123)
|
d2 := NewFromFloat(500.123)
|
||||||
|
|
||||||
|
|
90
decimal_gen.go
Normal file
90
decimal_gen.go
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package decimal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errShortBytes = errors.New("msgp: too few bytes left to read object")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// MarshalMsg implements msgp.Marshaler
|
||||||
|
func (d Decimal) MarshalMsg(b []byte) (o []byte, err error) {
|
||||||
|
o = require(b, d.Msgsize())
|
||||||
|
str := d.String()
|
||||||
|
sz := len(str)
|
||||||
|
if sz > 30 {
|
||||||
|
sz = 30
|
||||||
|
if str[29] == '.' {
|
||||||
|
sz = 29
|
||||||
|
}
|
||||||
|
|
||||||
|
str = str[:sz]
|
||||||
|
}
|
||||||
|
|
||||||
|
o, n := ensure(b, 1+sz)
|
||||||
|
o[n] = byte(0xa0 | sz)
|
||||||
|
n++
|
||||||
|
|
||||||
|
return o[:n+copy(o[n:], str)], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalMsg implements msgp.Unmarshaler
|
||||||
|
func (d *Decimal) UnmarshalMsg(b []byte) (o []byte, err error) {
|
||||||
|
l := len(b)
|
||||||
|
if l < 1 {
|
||||||
|
return nil, errShortBytes
|
||||||
|
}
|
||||||
|
|
||||||
|
sz := int(b[0] & 0x1f)
|
||||||
|
if len(b[1:]) < sz {
|
||||||
|
err = errShortBytes
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if *d, err = NewFromString(string(b[1 : sz+1])); err == nil {
|
||||||
|
o = b[sz:]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
|
||||||
|
func (d Decimal) Msgsize() int {
|
||||||
|
return 31
|
||||||
|
}
|
||||||
|
|
||||||
|
// Require ensures that cap(old)-len(old) >= extra.
|
||||||
|
func require(old []byte, extra int) []byte {
|
||||||
|
l := len(old)
|
||||||
|
c := cap(old)
|
||||||
|
r := l + extra
|
||||||
|
if c >= r {
|
||||||
|
return old
|
||||||
|
} else if l == 0 {
|
||||||
|
return make([]byte, 0, extra)
|
||||||
|
}
|
||||||
|
// the new size is the greater
|
||||||
|
// of double the old capacity
|
||||||
|
// and the sum of the old length
|
||||||
|
// and the number of new bytes
|
||||||
|
// necessary.
|
||||||
|
c <<= 1
|
||||||
|
if c < r {
|
||||||
|
c = r
|
||||||
|
}
|
||||||
|
n := make([]byte, l, c)
|
||||||
|
copy(n, old)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure 'sz' extra bytes in 'b' btw len(b) and cap(b)
|
||||||
|
func ensure(b []byte, sz int) ([]byte, int) {
|
||||||
|
l := len(b)
|
||||||
|
c := cap(b)
|
||||||
|
if c-l < sz {
|
||||||
|
o := make([]byte, (2*c)+sz) // exponential growth
|
||||||
|
n := copy(o, b)
|
||||||
|
return o[:n+sz], n
|
||||||
|
}
|
||||||
|
return b[:l+sz], l
|
||||||
|
}
|
132
decimal_test.go
132
decimal_test.go
|
@ -2,6 +2,7 @@ package decimal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -480,6 +481,7 @@ func TestNewFromInt(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
9223372036854775807: "9223372036854775807",
|
9223372036854775807: "9223372036854775807",
|
||||||
|
-9223372036854775808: "-9223372036854775808",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -505,6 +507,7 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
2147483647: "2147483647",
|
2147483647: "2147483647",
|
||||||
|
-2147483648: "-2147483648",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -524,6 +527,25 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewFromUint64(t *testing.T) {
|
||||||
|
tests := map[uint64]string{
|
||||||
|
0: "0",
|
||||||
|
1: "1",
|
||||||
|
323412345: "323412345",
|
||||||
|
9223372036854775807: "9223372036854775807",
|
||||||
|
18446744073709551615: "18446744073709551615",
|
||||||
|
}
|
||||||
|
|
||||||
|
for input, s := range tests {
|
||||||
|
d := NewFromUint64(input)
|
||||||
|
if d.String() != s {
|
||||||
|
t.Errorf("expected %s, got %s (%s, %d)",
|
||||||
|
s, d.String(),
|
||||||
|
d.value.String(), d.exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewFromBigIntWithExponent(t *testing.T) {
|
func TestNewFromBigIntWithExponent(t *testing.T) {
|
||||||
type Inp struct {
|
type Inp struct {
|
||||||
val *big.Int
|
val *big.Int
|
||||||
|
@ -2395,104 +2417,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")
|
||||||
|
@ -3673,3 +3648,18 @@ func ExampleNewFromFloat() {
|
||||||
//0.123123123123123
|
//0.123123123123123
|
||||||
//-10000000000000
|
//-10000000000000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMsgPack(t *testing.T) {
|
||||||
|
d, _ := NewFromString("79228162514264337593543950334.123222")
|
||||||
|
out, err := d.MarshalMsg(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(base64.StdEncoding.EncodeToString(out))
|
||||||
|
|
||||||
|
var d2 = Decimal{}
|
||||||
|
if _, err = d2.UnmarshalMsg(out); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(d2.String())
|
||||||
|
}
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -1,3 +1,3 @@
|
||||||
module github.com/shopspring/decimal
|
module github.com/piyongcai/decimal
|
||||||
|
|
||||||
go 1.7
|
go 1.10
|
||||||
|
|
Loading…
Reference in a new issue