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

number_with_delimiter method? (thousands separators) #5573

Closed
ghost opened this issue Jan 11, 2018 · 7 comments
Closed

number_with_delimiter method? (thousands separators) #5573

ghost opened this issue Jan 11, 2018 · 7 comments

Comments

@ghost
Copy link

ghost commented Jan 11, 2018

I searched for some time in the API docs and in the source code but could not find a function to format numbers with thousands separators (something like Rails' number_to_delimiter helper).

I'm using this snippet for now (which I adapted from ActiveSupport's source code):

def number_with_separator(number)
  sprintf("%0.2f", number).gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end

number_with_separator 12345678.09
# => "12,345,678.09"

I wonder if something like that shouldn't be included in the standard lib?

@hugoabonizio
Copy link
Contributor

hugoabonizio commented Jan 11, 2018

I think this belongs to a i18n shard, it's useful for presentations but it differs between localizations.

@RX14
Copy link
Contributor

RX14 commented Jan 11, 2018

Should Int#to_s go in a shard because it uses . as a decimal separator? Perhaps providing the very most common format in the stdlib and leaving others to implement i18n if required would be the best.

@hugoabonizio
Copy link
Contributor

I'm actually referring to thousands separator ,, for decimal separator there already is just sprintf("%0.2f", number).

@straight-shoota
Copy link
Member

Yeah this is related to i18n but the implementation of the formatter itself could very well be in the standard library. It just needs to be configureable as to which characters to use as thousands delimiter and decimal separator.
A i18n shard could just wrap that method providing these characters based on a locale.

@hugoabonizio
Copy link
Contributor

hugoabonizio commented Jan 12, 2018

Could use _ as default separator (12_345_678.09) to feel more natural, the language itself has this construction.

@straight-shoota
Copy link
Member

Resolved by #6314

@HertzDevil
Copy link
Contributor

Can this be closed now?

@asterite asterite closed this as completed Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants