Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NewFromUint64 #352

Merged
merged 2 commits into from Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions decimal.go
Expand Up @@ -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
func NewFromBigInt(value *big.Int, exp int32) Decimal {
return Decimal{
Expand Down
37 changes: 29 additions & 8 deletions decimal_test.go
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down