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

Rounding options to toRelative #1685

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Shiva127
Copy link

@Shiva127 Shiva127 commented Feb 19, 2025

This PR fully address the issue described in #1129 (and fully replace PR #1683) by introducing an additional rounding option. This PR is fully backward compatible.

Problem

Before this PR, the only option for rounding with toRelative() is to rounds towards zero.

Solution

With this PR, I add a new parameter rounding who can be "towardsZero", "awayFromZero", "round", "floor", or "ceil" to address all options. The rounding option works with round: true and round: false.

I'm open for better names for "towardsZero", "awayFromZero".

Fixes #1129.

`diffRelative(options)` is the only function using the 3rd parameters.
@hugohutri
Copy link

Nice! Good that you implemented the remaining rounding methods as well 😎
"towardsZero" and "awayFromZero" are pretty descriptive and I don't know if there are good alternatives. If we really want some other names for those, then "towardsZero" could be "truncate" or "clip" but awayFromZero is a bit harder to name imo 🤔 Maybe "expand" ?
But looks good anyway!

@Shiva127
Copy link
Author

Shiva127 commented Feb 20, 2025

Nice! Good that you implemented the remaining rounding methods as well 😎

Thank you!

"towardsZero" and "awayFromZero" are pretty descriptive and I don't know if there are good alternatives.

Yes, it's descriptive but I don't know if there is a more accurate mathematical name for that.

If we really want some other names for those, then "towardsZero" could be "truncate" or "clip" but awayFromZero is a bit harder to name imo 🤔 Maybe "expand" ? But looks good anyway!

In this case I find "towardsZero" and "awayFromZero" more explicit than "truncate", "clip", or "expand".

@Shiva127
Copy link
Author

I've found the rounding modes of Intl.NumberFormat() function and they use "expand" for "awayFromZero" and "trunc" for "towardsZero".

@hugohutri You were right. 😉

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.

More control over DateTime#toRelative rounding behavior
2 participants