decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Rounding issue (expect round up, but down)

Open tiendc opened this issue 1 year ago • 1 comments

I have an issue with rounding numbers.

d, _ := decimal.New(0, 0).SetString("1.45")
fmt.Println(d.Round(2)) -> Expect: 1.5 actual: 1.4

d, _ := decimal.New(0, 0).SetString("1.5")
fmt.Println(d.Round(1)) -> Expect: 2 actual: 2 (seems not consistent with the above case)

d, _ := decimal.New(0, 0).SetString("1.451")
fmt.Println(d.Round(2)) -> Expect: 1.5 actual: 1.5 (correct)

Any investigation? thanks.

tiendc avatar Sep 13 '22 07:09 tiendc

You can use the context rounding mode to configure Round(...) et al. https://pkg.go.dev/github.com/ericlagergren/decimal#Context

The default mode is roundTiesToEven and, as I see things, conforming to https://en.wikipedia.org/wiki/IEEE_754-1985#Rounding_floating-point_numbers ("Round to Nearest")

lotodore avatar Sep 28 '22 08:09 lotodore