Compare commits

...

6 commits

Author SHA1 Message Date
Lanre Ade
a7f8d8f947
Merge a3267013b7 into 80ec940054 2024-04-08 23:47:21 +00:00
Egor Seredin
80ec940054
Add NewFromUint64 constructor (#352) 2024-04-08 16:38:20 +02:00
Mateusz Woś
2fa107d0fe
Remove production usage from docs (#362) 2024-04-07 20:33:44 +02:00
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
Lanre Ade
a3267013b7 Update decimal.go 2019-03-03 16:41:08 -07:00
5 changed files with 50 additions and 19 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
@ -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?

View file

@ -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{
@ -1406,9 +1418,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.
@ -1819,6 +1829,11 @@ func (d Decimal) MarshalBinary() (data []byte, err error) {
// Scan implements the sql.Scanner interface for database deserialization.
func (d *Decimal) Scan(value interface{}) error {
if value == nil {
d = nil
return nil
}
// first try to see if the data is stored in database as a Numeric datatype
switch v := value.(type) {

View file

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

2
go.mod
View file

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