decimal icon indicating copy to clipboard operation
decimal copied to clipboard

UnmarshalText doesn't reset the number before parsing

Open ab0455a08d03 opened this issue 2 years ago • 1 comments

If UnmarshalText is called multiple times for the same decimal.Big instance, the input is appended to the internal state instead of overriding previous input.

package decimal_test

import (
	"testing"

	"github.com/ericlagergren/decimal"
)

func TestUnmarshalTest(t *testing.T) {
	x := &decimal.Big{}

	x.UnmarshalText([]byte(`1`))
	
	x.UnmarshalText([]byte(`2`))
	if x.String() != "2" {
		t.Errorf("expect 1 got %s", x.String())
	}
}

Test run:

--- FAIL: TestUnmarshalTest (0.00s)
    decimal_test.go:19: expect 1 got 12
FAIL
FAIL	decimals	0.001s
**FAIL**

ab0455a08d03 avatar Feb 02 '24 16:02 ab0455a08d03

Could be related to #185 - something's wrong with reusing decimal.Big from strings.

comunidadio avatar Feb 03 '24 00:02 comunidadio