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 formatting with abbreviations: $1,200,000 -> $1.2MM, $5,586 -> $5.6k, etc. #26

Closed
micahwedemeyer opened this issue Jan 23, 2017 · 3 comments

Comments

@micahwedemeyer
Copy link

I'm just wondering if it's possible to use this package to round/convert to "business-y" numbers. I'd like to display larger values, often currency, and use the abbreviated forms.

I can easily round on my own (ie. 5586 -> 5.6 x 1000), but it would be great if there were a way to then get the abbreviation suffix (ie. "MM", "k", "B"?) I guess once I list them out, there's only those 3 (and therefore not that hard), but I'm still wondering if there's anything built-in.

@danielberkompas
Copy link
Owner

See Number.Human.number_to_human/2. The unit names aren't currently configurable, (which should be changed) but you can use it like this:

count
|> number_to_human(precision: 1)
|> String.replace(" Thousand", "K")
|> String.replace(" Million", "M")
|> String.replace(" Billion", "B")
|> String.replace(" Trillion", "T")
|> String.replace(" Quadrillion", "Q")

@micahwedemeyer
Copy link
Author

Sweet, this is great! 👍

@sn3p
Copy link

sn3p commented May 12, 2021

This would be a nice to have build in.

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

No branches or pull requests

3 participants