Open mitar opened 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.
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.
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.
+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:
>> sprintf("%2f", 44, {thousandsSeparator: true})
>> sprintf("%2f", 44, {format: "en-US"})
toLocaleString()
method of the returned string>> "12345.678".toLocaleString()
"12345.678"
>> sprintf("%.2f", 12345.67)
"12345.67"
>> sprintf("%.2f", 12345.67).toLocaleString()
"12,345.67"
>> sprintf(",%.2f", 12345.67)
"12,345.67"
>> sprintf("_%.2f", 12345.67) # typescript notation
"12,345.67"
>> sprintf("'%.2f", 12345.67) # bash printf notation, I think
"12,345.67"
It would be great to be able to print large numbers like 1234567890 into 1,234,567,890.