From 19b27aafdc874ad21d3240f0220b3f3fdc941409 Mon Sep 17 00:00:00 2001 From: Egor Seredin <4819888+agmt@users.noreply.github.com> Date: Tue, 13 Feb 2024 06:52:50 +0000 Subject: [PATCH] Add NewFromUint64 --- decimal.go | 12 ++++++++++++ decimal_test.go | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/decimal.go b/decimal.go index e73b379..e91bcd4 100644 --- a/decimal.go +++ b/decimal.go @@ -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{ diff --git a/decimal_test.go b/decimal_test.go index 17d4627..92e5348 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -524,6 +524,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