mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 12:50:49 +01:00
Compare commits
1 commit
a7f8d8f947
...
b3818e7f64
Author | SHA1 | Date | |
---|---|---|---|
|
b3818e7f64 |
5 changed files with 19 additions and 45 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
@ -9,7 +9,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go: [ '1.10.x', '1.19', '1.20', '1.21', '1.22', '1.x' ]
|
go: [ '1.7.x', '1.18', '1.19', '1.20', '1.21', '1.x' ]
|
||||||
name: Go ${{ matrix.go }}
|
name: Go ${{ matrix.go }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
|
@ -22,7 +22,7 @@ Run `go get github.com/shopspring/decimal`
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
Decimal library requires Go version `>=1.10`
|
Decimal library requires Go version `>=1.7`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -63,6 +63,11 @@ func main() {
|
||||||
|
|
||||||
http://godoc.org/github.com/shopspring/decimal
|
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
|
## FAQ
|
||||||
|
|
||||||
#### Why don't you just use float64?
|
#### Why don't you just use float64?
|
||||||
|
|
16
decimal.go
16
decimal.go
|
@ -130,18 +130,6 @@ 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{
|
||||||
|
@ -1418,7 +1406,9 @@ 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)
|
||||||
return scaledD.value
|
i := &big.Int{}
|
||||||
|
i.SetString(scaledD.String(), 10)
|
||||||
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// BigFloat returns decimal as BigFloat.
|
// BigFloat returns decimal as BigFloat.
|
||||||
|
|
|
@ -480,7 +480,6 @@ func TestNewFromInt(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
9223372036854775807: "9223372036854775807",
|
9223372036854775807: "9223372036854775807",
|
||||||
-9223372036854775808: "-9223372036854775808",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -506,7 +505,6 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
2147483647: "2147483647",
|
2147483647: "2147483647",
|
||||||
-2147483648: "-2147483648",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -526,25 +524,6 @@ 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
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
||||||
module github.com/shopspring/decimal
|
module github.com/shopspring/decimal
|
||||||
|
|
||||||
go 1.10
|
go 1.7
|
||||||
|
|
Loading…
Reference in a new issue