From 1df8fb30155b21e08788cfa53c495e7a0d65f12a Mon Sep 17 00:00:00 2001 From: zlasd Date: Sun, 31 Dec 2023 20:11:33 +0800 Subject: [PATCH] Ensure empty Decimal returns correct zero-value NumDigits (#301) --- decimal.go | 1 + decimal_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/decimal.go b/decimal.go index 8014910..6f7c41e 100644 --- a/decimal.go +++ b/decimal.go @@ -928,6 +928,7 @@ func (d Decimal) Ln(precision int32) (Decimal, error) { // NumDigits returns the number of digits of the decimal coefficient (d.Value) // Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part func (d Decimal) NumDigits() int { + d.ensureInitialized() // Note(mwoss): It can be optimized, unnecessary cast of big.Int to string if d.IsNegative() { return len(d.value.String()) - 1 diff --git a/decimal_test.go b/decimal_test.go index 2df9591..7dc5be6 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -2829,6 +2829,7 @@ func TestDecimal_NumDigits(t *testing.T) { {"-5.26", 3}, {"-5.2663117716", 11}, {"-26.1", 3}, + {"", 1}, } { d, _ := NewFromString(testCase.Dec)