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

proposal: x/text: provide an option for compact number formatting #34989

Open
nkall opened this issue Oct 18, 2019 · 2 comments
Open

proposal: x/text: provide an option for compact number formatting #34989

nkall opened this issue Oct 18, 2019 · 2 comments

Comments

@nkall
Copy link

nkall commented Oct 18, 2019

The Unicode Technical Standard specifies a list of long and short Compact Number Formats for use in formatting truncated versions of numbers (e.g. 35K, 2M). For reference, the JSON representation of these formats in English can be viewed here. x/text implements a lot of the standard, including currency and number formatting, but there isn't currently a way to get compact formats as described.

I suggest x/text/number be given a Compact Option which can be created with either a "long" or "short" typed argument. When this option is provided and the number formatted is suitable for compacting in that locale, the compact version should instead be returned.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2019

/cc @mpvl

@rsc rsc added this to Incoming in Proposals (old) Dec 4, 2019
@mpvl
Copy link
Contributor

mpvl commented Jul 7, 2020

This naturally fits within x/text.

The infrastructure to parse and organize the needed data is present in x/text's internal packages.

I would just not make it an option, but rather a Formatter. This way formatting w compact number would read:

p := message.NewPrinter(language.English)

p.Printf("%v bottles of beer on the wall.", number.Compact(3000000))
// Prints: 3M bottles of beer on the wall.

This would default to english printing when used with the fmt package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants