Skip to content

Commit

Permalink
Fix rounding in BigDecimal.SetScale for golang.
Browse files Browse the repository at this point in the history
Golang "floors" negative numbers to the next lower
number, while the PAP pseudo code assumes we're
dealing with Java which "floors" negatives to the
next higher number, i.e.:
Java: Floor(-4.5) == -4
Golang: Floor(-4.5) == -5

Thanks Markus!
  • Loading branch information
jenner committed Sep 14, 2023
1 parent 1919f00 commit 323b9af
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lstgen/generators/golang/BigDecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ func (d BigDecimal) SetScale(scale int, rounding int) BigDecimal {

var v float64
if rounding == ROUND_DOWN {
v = math.Floor(d.V * exp)
if d.V < 0 {
v = math.Ceil(d.V * exp)
} else {
v = math.Floor(d.V * exp)
}
} else if rounding == ROUND_UP {
v = math.Ceil(d.V * exp)
if d.V < 0 {
v = math.Floor(d.V * exp)
} else {
v = math.Ceil(d.V * exp)
}
} else {
v = math.Round(d.V * exp)
}
Expand All @@ -77,4 +85,4 @@ func NewFromInt(value int64) BigDecimal {

func NewFromFloat(value float64) BigDecimal {
return BigDecimal{value}
}
}

0 comments on commit 323b9af

Please sign in to comment.