Ensure empty Decimal returns correct zero-value NumDigits (#301)

This commit is contained in:
zlasd 2023-12-31 20:11:33 +08:00 committed by GitHub
parent 572d78e32d
commit 1df8fb3015
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 0 deletions

View file

@ -928,6 +928,7 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
// NumDigits returns the number of digits of the decimal coefficient (d.Value) // 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 // Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part
func (d Decimal) NumDigits() int { func (d Decimal) NumDigits() int {
d.ensureInitialized()
// Note(mwoss): It can be optimized, unnecessary cast of big.Int to string // Note(mwoss): It can be optimized, unnecessary cast of big.Int to string
if d.IsNegative() { if d.IsNegative() {
return len(d.value.String()) - 1 return len(d.value.String()) - 1

View file

@ -2829,6 +2829,7 @@ func TestDecimal_NumDigits(t *testing.T) {
{"-5.26", 3}, {"-5.26", 3},
{"-5.2663117716", 11}, {"-5.2663117716", 11},
{"-26.1", 3}, {"-26.1", 3},
{"", 1},
} { } {
d, _ := NewFromString(testCase.Dec) d, _ := NewFromString(testCase.Dec)