Compare commits

...

3 commits

Author SHA1 Message Date
Egor Seredin
b5d48e3f91
Merge dd34943dc4 into d00399e161 2024-02-19 21:37:10 -07: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
2 changed files with 41 additions and 8 deletions

View file

@ -116,6 +116,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 // 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{

View file

@ -480,6 +480,7 @@ func TestNewFromInt(t *testing.T) {
1: "1", 1: "1",
323412345: "323412345", 323412345: "323412345",
9223372036854775807: "9223372036854775807", 9223372036854775807: "9223372036854775807",
-9223372036854775808: "-9223372036854775808",
} }
// add negatives // add negatives
@ -505,6 +506,7 @@ func TestNewFromInt32(t *testing.T) {
1: "1", 1: "1",
323412345: "323412345", 323412345: "323412345",
2147483647: "2147483647", 2147483647: "2147483647",
-2147483648: "-2147483648",
} }
// add negatives // 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) { func TestNewFromBigIntWithExponent(t *testing.T) {
type Inp struct { type Inp struct {
val *big.Int val *big.Int