Compare commits

...

6 commits

Author SHA1 Message Date
Sergey Vasiliev
3b53264ada
Merge 6e54f647f0 into 645a76e5b0 2024-04-06 22:59:00 -02:30
Philip Dubé
645a76e5b0
Optimize BigInt (#359) 2024-04-05 21:43:23 +02:00
Mateusz Woś
ed8f76e893
Drop support for Go versions older than 1.10 (#361)
* Drop support for Go versions older than 1.10
* Remove 1.18 from test suite
2024-04-05 21:41:05 +02:00
vasilbekk
6e54f647f0 Newline in end file 2022-10-01 14:20:16 +03:00
vasilbekk
d23c457004 Added unittests 2022-10-01 14:02:56 +03:00
vasilbekk
ba54fe1418 Added varible StringTrimTrailingZeros and comment for it 2022-10-01 13:47:11 +03:00
5 changed files with 58 additions and 7 deletions

View file

@ -9,7 +9,7 @@ jobs:
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

View file

@ -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

View file

@ -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
@ -1406,9 +1414,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.
@ -1459,7 +1465,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

View file

@ -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())
}
}
}

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/shopspring/decimal
go 1.7
go 1.10