mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 12:30:49 +01:00
Ensure empty Decimal returns correct zero-value NumDigits (#301)
This commit is contained in:
parent
572d78e32d
commit
1df8fb3015
2 changed files with 2 additions and 0 deletions
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue