-
Notifications
You must be signed in to change notification settings - Fork 163
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
Improved big-number formatting #346
Conversation
- [IntegerFormatStyle] I removed the trapping conversion to `Int`. IntegerFormatStyle can format big integers since (swiftlang#262). - [FloatingPointFormatStyle] I removed the rounding conversion to `Double`. `formatted()` now does whatever `format(_:)` does. - [Decimal.FormatStyle] N/A (there were no conversions here).
- IntegerFormatStyle big integer tests succeed. - IntegerFormatStyle.Attributed big integer tests fail (output is clamped to `Int64`).
- Added a numeric string representation case to ICUNumberFormatter.Value. - IntegerFormatStyle.Attributed now uses the above instead of `Int64(clamping:)`.
…g#186). BinaryInteger's numeric string representation supersedes Decimal in the following cases: 1. IntegerFormatStyle. 2. integerFormatStyle.Attributed. 3. IntegerFormatStyle.Currency. 4. IntegerFormatStyle.Percent.
The numeric string format permits redundant zeros (like `+00.00`).
Just to be clear: this pull request is a smörgåsbord. I wanted to fix the trapping conversion (and I suggest you keep that part). But, after that, I reenabled some of your tests and looked for ways to fix |
I don't want to bombard this thread. But after looking more closely at it, I see that Edit: here's a separate commit with this approach ( |
Both `isZero` and `doubleValue` were used in `ByteCountFormatStyle`. These values are now taken from `FormatInput` (`Int64`) instead. Removing them from `ICUNumberFormatter.Value` makes it easier to accommodate non-numeric payloads such as strings, which can be used to format arbitrary precision numbers.
Thank you! I think this is indeed the most reasonable approach. I'd be ok either if you include that as part of this change or a separate PR. No bombarding at all either |
Sources/FoundationInternationalization/Formatting/Number/ICUNumberFormatter.swift
Outdated
Show resolved
Hide resolved
Cool. I've added f065a2d to this pull request. |
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.
@swift-ci please test
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! Thanks!
@swift-ci please test |
Sources/FoundationInternationalization/Formatting/ByteCountFormatStyle.swift
Show resolved
Hide resolved
realized there's one pending point. Dismissing the review so we don't miss it
Here's an internal method simliar to the `_format(_:)` method removed earlier because wants its method back! I removed the first method to accommodate `ICUNumberFormatter.Value` cases that cannot implement `doubleValue`. The new method parameterizes the conversion instead, so you can call it whenever conversions to `Double` are possible.
ba8bf28
to
f065a2d
Compare
Here's a solution that lets you use Double in ByteCountFormatStyle: _format(.integer(int64), doubleValue: Double(int64))
_format(.floatingPoint(double), doubleValue: double) If you think it's a reasonable approach, I'll add it to this pull request. |
Hm. I suppose I'll add the proposed solution and amend it if needed. I think that might be easier. |
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'll let @itingliu have the final say here but this looks good to 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.
LGTM!
@swift-ci please test |
CI is down due to a regression in recent toolchain. That should be fixed in swiftlang/swift#70559. I'll rerun the test after CI updates to a newer toolchain containing the fix |
@swift-ci please test |
Howdy! I've made some changes to improve your big-number formatting.
Changes
Comments
[RESOLVED] I was not sure what to do with ICUNumberFormatterBase.Value's isZero and doubleValue – please share your thoughts. I solved it ™️ by force unwrapping Double.init(_:). The force unwrap should never fail, assuming the numeric string representation is well-formed, but large values may result in ± Double.infinity. It is not a problem for isZero, but I do not know the semantics of doubleValue.
Alternatives
[OUTDATED] You can add some thing like fallbackInteger(any BinaryInteger) instead of numericStringRepresentation(String). Perhaps it makes sense if you don't want to check isZero through Double.