Compare commits

...

4 commits

Author SHA1 Message Date
Egor Seredin
31802d80cb
Merge dd34943dc4 into 2fa107d0fe 2024-04-08 00:19:25 +02:00
Mateusz Woś
2fa107d0fe
Remove production usage from docs (#362) 2024-04-07 20:33:44 +02:00
Egor Seredin
dd34943dc4 Test: NewFromInt: add MinInt case 2024-02-13 06:59:58 +00:00
Egor Seredin
19b27aafdc Add NewFromUint64 2024-02-13 06:59:34 +00:00
3 changed files with 41 additions and 13 deletions

View file

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

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