Skip to content

Add .to_s('F') digit grouping for integer part - #264

Merged
mrkn merged 2 commits into
ruby:masterfrom
cryptogopher:master
Jul 5, 2023
Merged

mrkn merged 2 commits into
ruby:masterfrom
cryptogopher:master

Conversation

@cryptogopher

@cryptogopher cryptogopher commented Jul 1, 2023 •

Copy link
Copy Markdown

Right now to_s('F') digit grouping works properly only for fractional part. For integer part result is unspecified. In reality we also get grouping, but of unpractical nature:

# ruby -e "require 'bigdecimal'; puts BigDecimal('1234567.1234567').to_s('3F')"
123 456 7.123 456 7

This patch fixes grouping of integer part:

# ruby -e "require 'bigdecimal'; puts BigDecimal('1234567.1234567').to_s('3F')"
1 234 567.123 456 7

I consider this change backwards compatible as:
a) docs say only about grouping fractional part, no declarations about grouping integer part,
b) total string length without changes,
c) integer part grouping didn't make sense so there should be no or not much real life dependencies.

Also added some tests.

Comment thread test/bigdecimal/test_bigdecimal.rb Resolved
@mrkn
mrkn self-requested a review July 5, 2023 20:00
@zzak

zzak commented Jul 8, 2023

Copy link
Copy Markdown
Member

Looks like this broke some Active Support tests:
rails/rails#48693

@nobu

nobu commented Jul 8, 2023

Copy link
Copy Markdown
Member

This change does not related to ruby versions.
Before bad habit that testing this by RUBY_VERSION will spread more, we should bump up BigDecimal::VERSION.

@nobu nobu mentioned this pull request Jul 8, 2023
zzak added a commit to zzak/rails that referenced this pull request Jul 9, 2023
The previous examples were causing failures in CI, after ruby/bigdecimal#264 was merged.

For example:

```
Failure:
NumericExtFormattingTest#test_default_to_fs [/rails/activesupport/test/core_ext/numeric_ext_test.rb:446]:
--- expected
+++ actual
@@ -1 +1,3 @@
-"10000 10.0"
+# encoding: US-ASCII
+#    valid: true
+"10 00010.0"
```

The recommendation was to adjust the test to deal with the upstream change more flexibly.
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.

4 participants