shopspring / decimal

Arbitrary-precision fixed-point decimal numbers in Go
Other
6.41k stars 623 forks source link

calling DivRound on a negative number returns a positive number #385

Open fawkesley opened 1 month ago

fawkesley commented 1 month ago

Thanks for this library - happy user for years.

Discovered something today unexpected:

a := decimal.NewFromFloat(-35.0)
fmt.Println(a.DivRound(decimal.NewFromFloat(5.0))

7

This is different from Go's default behaviour:

fmt.Println(-35.0 / 5)

-7.0

Am I missing something? This seems fundamentally incorrect behaviour.

Is there a way divide and preserve sign?

Cheers Paul