Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding issue (expect round up, but down) #180

Open
tiendc opened this issue Sep 13, 2022 · 1 comment
Open

Rounding issue (expect round up, but down) #180

tiendc opened this issue Sep 13, 2022 · 1 comment

Comments

@tiendc
Copy link

tiendc commented Sep 13, 2022

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.

@lotodore
Copy link
Contributor

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants