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

Allow choice of rounding behavior for ties #12

Merged
merged 1 commit into from
Mar 17, 2024
Merged

Conversation

danielparks
Copy link
Owner

@danielparks danielparks commented Mar 16, 2024

Traditionally, rounding a value that is exactly halfway between to round values results in the higher round value:

10 == 5.round_to(10, Tie::Up)

This adds a parameter and enum to allow other rules for dealing with “ties” — values that are halfway between two round numbers. The rule mentioned above is represented by Tie::Up — ties choose the higher number. The complete list of options is:

  • Tie::Up
  • Tie::Down
  • Tie::TowardZero
  • Tie::AwayFromZero
  • Tie::TowardEven
  • Tie::TowardOdd

Fixes #3 — Support other types of rounding.


To do

  • Better docs
  • More complete tests
  • Remove FIXMEs
  • Changelog
  • Decide if there should be convenience methods

@danielparks danielparks force-pushed the tie-behavior branch 2 times, most recently from bfa4621 to 5b16b13 Compare March 17, 2024 05:20
@danielparks danielparks changed the title Various “tie” behaviors, e.g. round half toward zero Allow choice of rounding behavior for ties Mar 17, 2024
@danielparks danielparks force-pushed the tie-behavior branch 3 times, most recently from 9d77c1b to cebd5d4 Compare March 17, 2024 06:00
Traditionally, rounding a value that is exactly halfway between to round
values results in the higher round value:

    10 == 5.round_to(10, Tie::Up)

This adds a parameter and `enum` to allow other rules for dealing with
“ties” — values that are halfway between two round numbers. The rule
mentioned above is represented by `Tie::Up` — ties choose the higher
number. The complete list of options is:

  * `Tie::Up`
  * `Tie::Down`
  * `Tie::TowardZero`
  * `Tie::AwayFromZero`
  * `Tie::TowardEven`
  * `Tie::TowardOdd`

Fixes #3 — Support other types of rounding.
@danielparks
Copy link
Owner Author

round_up_to() would be nice, but let’s leave that for another PR.

@danielparks danielparks marked this pull request as ready for review March 17, 2024 06:09
@danielparks danielparks merged commit 7874992 into main Mar 17, 2024
5 checks passed
@danielparks danielparks deleted the tie-behavior branch March 17, 2024 06:09
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

Successfully merging this pull request may close these issues.

Support other types of rounding
1 participant