Skip to content

Inconsistent (or buggy) Quantize behavior #122

Description

@LasTshaMAN

Looks like Quantize(result, amount, 0) func works differently for:

  • when amount is of the form N.0* (where N is integer number and N != 0)
  • when amount is of the form 0.0*

Here is a simple test to reproduce this (with v3.1.0):

// quantize is a simple wrapper around `apd` library.
func quantize(amount *apd.Decimal, decimalPlaces uint32, roundingRule apd.Rounder) (*apd.Decimal, error) {
	ctx := apd.BaseContext.WithPrecision(infinitePrecision)
	ctx.Rounding = roundingRule

	var amt apd.Decimal
	exp := -int32(decimalPlaces)
	_, err := ctx.Quantize(&amt, amount, exp)

	return &amt, err
}

func TestQuantizeRounding(t *testing.T) {
        // 1.1 -> 2 (works as expected)
	got, err = quantize(apd.New(11, -1), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(2, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 1.01 -> 2 (works as expected)
	got, err := quantize(apd.New(101, -2), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want := apd.New(2, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 0.1 -> 1 (works as expected)
	got, err = quantize(apd.New(1, -1), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(1, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 0.01 -> 1 (doesn't work as expected! instead it does 0.01 -> 0 for some reason)
	got, err = quantize(apd.New(1, -2), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(1, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions