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

Export RoundUp/RoundDown method #191

Closed
lovung opened this issue Nov 30, 2020 · 2 comments
Closed

Export RoundUp/RoundDown method #191

lovung opened this issue Nov 30, 2020 · 2 comments

Comments

@lovung
Copy link
Contributor

lovung commented Nov 30, 2020

We only have Round method at

decimal/decimal.go

Lines 792 to 819 in 480f653

// Round rounds the decimal to places decimal places.
// If places < 0, it will round the integer part to the nearest 10^(-places).
//
// Example:
//
// NewFromFloat(5.45).Round(1).String() // output: "5.5"
// NewFromFloat(545).Round(-1).String() // output: "550"
//
func (d Decimal) Round(places int32) Decimal {
// truncate to places + 1
ret := d.rescale(-places - 1)
// add sign(d) * 0.5
if ret.value.Sign() < 0 {
ret.value.Sub(ret.value, fiveInt)
} else {
ret.value.Add(ret.value, fiveInt)
}
// floor for positive numbers, ceil for negative numbers
_, m := ret.value.DivMod(ret.value, tenInt, new(big.Int))
ret.exp++
if ret.value.Sign() < 0 && m.Cmp(zeroInt) != 0 {
ret.value.Add(ret.value, oneInt)
}
return ret
}

How about exposing the RoundDown and RoundUp method?

@njason
Copy link
Member

njason commented Nov 30, 2020

There are Floor and Ceil, but they only round to integer values. Since adding optional parameters to these functions is not an option, I think it would make sense to add the new proposed functions. Added to backlog.

@mwoss
Copy link
Member

mwoss commented Jan 10, 2021

Closing, implemented in #196 and merged to master.

@mwoss mwoss closed this as completed Jan 10, 2021
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

3 participants