alexei / sprintf.js

sprintf.js is a complete open source JavaScript sprintf implementation
BSD 3-Clause "New" or "Revised" License
2.11k stars 291 forks source link

Thousands separator #124

Open mitar opened 7 years ago

mitar commented 7 years ago

It would be great to be able to print large numbers like 1234567890 into 1,234,567,890.

alexei commented 7 years ago

That would be interesting.

How should one specify the options? Should sprintf support all options, or only the locale? Also, I see the Intl API is not supported in all browsers.

mitar commented 7 years ago

I would not go the locale route, but maybe extend sprintf syntax in a way to allow formatting. Because I could want this for one number in a string, but not another.

alexei commented 7 years ago

I agree that one would sometimes need to yield a locale-aware and non locale-aware number at the same time, however I wouldn't do it without Intl. And as far as I can tell it doesn't have a way of setting a default locale, so I would need then to implement it either as a method or in the placeholder format (which is not desired). So I'd go for the former.

githorse commented 4 months ago

+1. I'm about to roll out a formatting solution based on this kickass library but I will definitely need thousands separators. Best I can tell this is not a shortcoming of sprint.js since there doesn't seem to be support for thousands separators in sprintf itself.

It's actually hard for me to imagine when somebody would not want this. What is the sprintf function for except to format a human-readable string? Which humans do not want thousands separators? (I'm sure somebody can come up with a counterexample.)

Personally I'm going to want the correct internationalization; I don't think I have a use case for picking my own separator, but perhaps there is one. Obviously doing so makes the syntax more complicated.

A few ideas:

options argument

override the prototype toLocaleString() method of the returned string

new format code