mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 12:30:49 +01:00
Improve performance of Abs() and Round() (#240)
This commit is contained in:
parent
cc4584f0a5
commit
483a047db9
1 changed files with 6 additions and 0 deletions
|
@ -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{
|
||||
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue