-
Notifications
You must be signed in to change notification settings - Fork 184
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
Add more functions to CompactDecimalFormatter #3699
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the failing tests.
} | ||
|
||
/// Formats an integer in compact decimal notation using the default | ||
/// precision settings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand correctly that if you want something fancier, e.g., at least three digits (like YT subscriber counts), or directed rounding (like everything at YT) you then need to round yourself?
(That works for me.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be okay if we landed this PR and then in the future we added such options to CompactDecimalFormatterOptions
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, though the rounding direction discussion would then come back to haunt us :-)
As I wrote, I am also fine with just not having those options, and seeing the sort of thing that YouTube does as an advanced do-your-own-rounding scenario; since we have format_compact_decimal and CompactDecimal, it is much easier for the caller to have their own arbitrarily fancy rounding logic than it was for me with ICU a few years ago.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up filed: #3929
) -> Result<FormattedCompactDecimal<'_>, CompactDecimalError> { | ||
use fixed_decimal::FloatPrecision::Floating; | ||
// NOTE: This first gets the shortest representation of the f64, which | ||
// manifests as double rounding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think this is actually a problem given that you do not give the user control over the rounding mode, and that the second rounding is only in the compact case: the two roundings are nearest ties to even, and the ties for the second rounding are representable unless you have really stupidly large numbers (being integers), so the first rounding will not produce a tie for the second.
The double rounding issue would manifest if you could request things such as « nearest, ties to even, exactly one sig. dec. »: then 95.0 / 100.0
, which is a little below 0.95, would round to 0.95 in the first rounding, and to 1.0 in the second, whereas it ought to round to 0.9.
Scripsit Shane:
No objections from me. |
This is mergeable, but I pulled the I decided for now to keep the function named |
(cherry picked from commit cd7d7f9)
🎉 All dependencies have been resolved ! |
Re-running CI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for these updates!
/// The result may have a fractional digit only if it is compact and its | ||
/// significand is less than 10. Trailing fractional 0s are omitted, and | ||
/// a sign is shown only for negative values. | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should document that the ryu feature is needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Part of #3647
Depends on #3945
The concerns about double rounding were discussed in #3647. I'd like to add a function that directly formats a
FixedDecimal
because otherwise this class is too hard to use.I also made the following changs:
FormattedFixedDecimal::get_compact_decimal
to get the result of the compact decimal conversionformat_f64
, which is fallible since f64-to-FixedDecimal is fallibleryu
feature to the CompactDecimalFormatter crateQuestions:
format
instead offormat_fixed_decimal
?