mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Compare commits
9 commits
241398860c
...
d996c65fad
Author | SHA1 | Date | |
---|---|---|---|
|
d996c65fad | ||
|
80ec940054 | ||
|
2fa107d0fe | ||
|
645a76e5b0 | ||
|
ed8f76e893 | ||
|
0e69d5cd53 | ||
|
bf7794e1a8 | ||
|
afac94241f | ||
|
aaa9a02fd2 |
6 changed files with 126 additions and 35 deletions
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -1,11 +1,15 @@
|
|||
name: ci
|
||||
on: [push]
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
jobs:
|
||||
ci-job:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
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 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
|
|
@ -22,7 +22,7 @@ Run `go get github.com/shopspring/decimal`
|
|||
|
||||
## Requirements
|
||||
|
||||
Decimal library requires Go version `>=1.7`
|
||||
Decimal library requires Go version `>=1.10`
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -63,11 +63,6 @@ func main() {
|
|||
|
||||
http://godoc.org/github.com/shopspring/decimal
|
||||
|
||||
## Production Usage
|
||||
|
||||
* [Spring](https://shopspring.com/), since August 14, 2014.
|
||||
* If you are using this in production, please let us know!
|
||||
|
||||
## FAQ
|
||||
|
||||
#### Why don't you just use float64?
|
||||
|
|
64
decimal.go
64
decimal.go
|
@ -9,7 +9,7 @@
|
|||
//
|
||||
// To use Decimal as part of a struct:
|
||||
//
|
||||
// type StructName struct {
|
||||
// type Struct struct {
|
||||
// Number Decimal
|
||||
// }
|
||||
//
|
||||
|
@ -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
|
||||
func NewFromBigInt(value *big.Int, exp int32) 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)
|
||||
// Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part
|
||||
func (d Decimal) NumDigits() int {
|
||||
d.ensureInitialized()
|
||||
// Note(mwoss): It can be optimized, unnecessary cast of big.Int to string
|
||||
if d.IsNegative() {
|
||||
return len(d.value.String()) - 1
|
||||
if d.value == nil {
|
||||
return 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.
|
||||
|
@ -1320,6 +1351,13 @@ func (d Decimal) LessThanOrEqual(d2 Decimal) bool {
|
|||
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:
|
||||
//
|
||||
// -1 if d < 0
|
||||
|
@ -1387,9 +1425,7 @@ func (d Decimal) IntPart() int64 {
|
|||
// BigInt returns integer component of the decimal as a BigInt.
|
||||
func (d Decimal) BigInt() *big.Int {
|
||||
scaledD := d.rescale(0)
|
||||
i := &big.Int{}
|
||||
i.SetString(scaledD.String(), 10)
|
||||
return i
|
||||
return scaledD.value
|
||||
}
|
||||
|
||||
// BigFloat returns decimal as BigFloat.
|
||||
|
@ -1522,7 +1558,7 @@ func (d Decimal) Round(places int32) Decimal {
|
|||
// NewFromFloat(545).RoundCeil(-2).String() // output: "600"
|
||||
// NewFromFloat(500).RoundCeil(-2).String() // output: "500"
|
||||
// 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 {
|
||||
if d.exp >= -places {
|
||||
return d
|
||||
|
@ -1547,7 +1583,7 @@ func (d Decimal) RoundCeil(places int32) Decimal {
|
|||
// NewFromFloat(545).RoundFloor(-2).String() // output: "500"
|
||||
// NewFromFloat(-500).RoundFloor(-2).String() // output: "-500"
|
||||
// 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 {
|
||||
if d.exp >= -places {
|
||||
return d
|
||||
|
@ -1572,7 +1608,7 @@ func (d Decimal) RoundFloor(places int32) Decimal {
|
|||
// NewFromFloat(545).RoundUp(-2).String() // output: "600"
|
||||
// NewFromFloat(500).RoundUp(-2).String() // output: "500"
|
||||
// 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 {
|
||||
if d.exp >= -places {
|
||||
return d
|
||||
|
@ -1599,7 +1635,7 @@ func (d Decimal) RoundUp(places int32) Decimal {
|
|||
// NewFromFloat(545).RoundDown(-2).String() // output: "500"
|
||||
// NewFromFloat(-500).RoundDown(-2).String() // output: "-500"
|
||||
// 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 {
|
||||
if d.exp >= -places {
|
||||
return d
|
||||
|
|
|
@ -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) {
|
||||
decimals := DecimalSlice([]Decimal{})
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
d2 := NewFromFloat(500.123)
|
||||
|
||||
|
|
|
@ -476,10 +476,11 @@ func TestNewFromFloatWithExponent(t *testing.T) {
|
|||
|
||||
func TestNewFromInt(t *testing.T) {
|
||||
tests := map[int64]string{
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
9223372036854775807: "9223372036854775807",
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
9223372036854775807: "9223372036854775807",
|
||||
-9223372036854775808: "-9223372036854775808",
|
||||
}
|
||||
|
||||
// add negatives
|
||||
|
@ -501,10 +502,11 @@ func TestNewFromInt(t *testing.T) {
|
|||
|
||||
func TestNewFromInt32(t *testing.T) {
|
||||
tests := map[int32]string{
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
2147483647: "2147483647",
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
2147483647: "2147483647",
|
||||
-2147483648: "-2147483648",
|
||||
}
|
||||
|
||||
// add negatives
|
||||
|
@ -524,6 +526,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) {
|
||||
type Inp struct {
|
||||
val *big.Int
|
||||
|
@ -2593,6 +2614,13 @@ func TestDecimal_Equalities(t *testing.T) {
|
|||
if c.LessThanOrEqual(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) {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
|||
module github.com/shopspring/decimal
|
||||
|
||||
go 1.7
|
||||
go 1.10
|
||||
|
|
Loading…
Reference in a new issue