mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Compare commits
6 commits
fe557f02fb
...
a19d52c58f
Author | SHA1 | Date | |
---|---|---|---|
|
a19d52c58f | ||
|
0e69d5cd53 | ||
|
bf7794e1a8 | ||
|
6e54f647f0 | ||
|
d23c457004 | ||
|
ba54fe1418 |
4 changed files with 116 additions and 12 deletions
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
|
@ -1,5 +1,9 @@
|
|||
name: ci
|
||||
on: [push]
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
jobs:
|
||||
ci-job:
|
||||
runs-on: ubuntu-latest
|
||||
|
|
41
decimal.go
41
decimal.go
|
@ -65,6 +65,14 @@ var PowPrecisionNegativeExponent = 16
|
|||
// silently lose precision.
|
||||
var MarshalJSONWithoutQuotes = false
|
||||
|
||||
// StringTrimTrailingZeros should be set to false if you want the decimal stringify without zeros trailing.
|
||||
// By default, when decimal is output as a string (for example, in JSON), zeros are truncated from it (2.00 -> 2, 3.11 -> 3.11, 13.000 -> 13).
|
||||
// But this logic can be changed by this variable.
|
||||
// For example, if you have numeric(10,2) values stored in your database,
|
||||
// and you want your API response to always be given 2 decimal places (even 2.00, 3.00, 17.00 [not 2,3,17]),
|
||||
// then this variable is a great way out.
|
||||
var StringTrimTrailingZeros = true
|
||||
|
||||
// ExpMaxIterations specifies the maximum number of iterations needed to calculate
|
||||
// precise natural exponent value using ExpHullAbrham method.
|
||||
var ExpMaxIterations = 1000
|
||||
|
@ -1224,14 +1232,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.
|
||||
|
@ -1440,7 +1467,7 @@ func (d Decimal) InexactFloat64() float64 {
|
|||
//
|
||||
// -12.345
|
||||
func (d Decimal) String() string {
|
||||
return d.string(true)
|
||||
return d.string(StringTrimTrailingZeros)
|
||||
}
|
||||
|
||||
// StringFixed returns a rounded fixed-point string with places digits after
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -3673,3 +3673,48 @@ func ExampleNewFromFloat() {
|
|||
//0.123123123123123
|
||||
//-10000000000000
|
||||
}
|
||||
|
||||
func TestDecimal_String(t *testing.T) {
|
||||
type testData struct {
|
||||
input string
|
||||
expected string
|
||||
}
|
||||
|
||||
tests := []testData{
|
||||
{"1.22", "1.22"},
|
||||
{"1.00", "1"},
|
||||
{"153.192", "153.192"},
|
||||
{"999.999", "999.999"},
|
||||
{"0.0000000001", "0.0000000001"},
|
||||
{"0.0000000000", "0"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d, err := NewFromString(test.input);
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if d.String() != test.expected {
|
||||
t.Errorf("expected %s, got %s", test.expected, d.String())
|
||||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
StringTrimTrailingZeros = true
|
||||
}()
|
||||
|
||||
StringTrimTrailingZeros = false
|
||||
tests = []testData{
|
||||
{"1.00", "1.00"},
|
||||
{"0.00", "0.00"},
|
||||
{"129.123000", "129.123000"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d, err := NewFromString(test.input);
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if d.String() != test.expected {
|
||||
t.Errorf("expected %s, got %s", test.expected, d.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue