Skip to content

Commit

Permalink
Ensure empty Decimal returns correct zero-value NumDigits (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlasd committed Dec 31, 2023
1 parent 572d78e commit 1df8fb3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions decimal.go
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions decimal_test.go
Expand Up @@ -2829,6 +2829,7 @@ func TestDecimal_NumDigits(t *testing.T) {
{"-5.26", 3},
{"-5.2663117716", 11},
{"-26.1", 3},
{"", 1},
} {
d, _ := NewFromString(testCase.Dec)

Expand Down

0 comments on commit 1df8fb3

Please sign in to comment.