Skip to content

Commit

Permalink
Improve performance of Abs() and Round() (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleDi committed Sep 9, 2021
1 parent cc4584f commit 483a047
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions decimal.go
Expand Up @@ -457,6 +457,9 @@ func (d Decimal) rescale(exp int32) Decimal {

// Abs returns the absolute value of the decimal.
func (d Decimal) Abs() Decimal {
if !d.IsNegative() {
return d
}
d.ensureInitialized()
d2Value := new(big.Int).Abs(d.value)
return Decimal{
Expand Down Expand Up @@ -879,6 +882,9 @@ func (d Decimal) StringFixedCash(interval uint8) string {
// NewFromFloat(545).Round(-1).String() // output: "550"
//
func (d Decimal) Round(places int32) Decimal {
if d.exp == -places {
return d
}
// truncate to places + 1
ret := d.rescale(-places - 1)

Expand Down

0 comments on commit 483a047

Please sign in to comment.