sprintf.js icon indicating copy to clipboard operation
sprintf.js copied to clipboard

Add support for custom number formatting

Open patkec opened this issue 8 years ago • 8 comments

Added support for custom number formatting. This way one can override the default formatting to support localization. Since localization usually depends on the environment (e.g. using Angular) the actual localization can as well be externalized.

Should help with #124.

patkec avatar May 09 '17 18:05 patkec

Like I said in the PR you linked, I wouldn't do it without Intl or something similar

alexei avatar May 10 '17 10:05 alexei

Rolling your own Intl seems a bit of an overkill, but fair enough. I'll use my modification for the time being and keep an eye on the #124 issue. But all in all, kudos for the lib, it's a great lib!

patkec avatar May 10 '17 14:05 patkec

I only suggested we shouldn't improvise

alexei avatar May 10 '17 15:05 alexei

Btw, my guess you're in need to format monetary values. If that's the case, may I suggest using a library that does just that? accounting.js looks good

alexei avatar May 10 '17 15:05 alexei

My use case is actually formatting numbers (not monetary) in an Angular application where we are using different locales depending on user settings. And there it's as easy as just using $filter('number')(value). That is why I thought that sprintf-js could just offer a simple extensibility point and then it's up to lib users what they do with it.

patkec avatar May 10 '17 15:05 patkec

Btw, are you using the sprintf filter in Angular? Just asking.

Re extensibility: I totally understand what you're saying. Though I'm not that happy with the implementation of this lib (made some bad decisions in my youth 😀), it pretty much does what it's supposed to do and so I'm satisfied. Also the npm stats are positive. However, about a month ago I started thinking that it might be the time to move on and surpass sprintf. I've been pondering with the idea of implementing a different formatter that's similar in style and concept to Python's new style string formatter where you have more control over the formatting of any type whether built in or not. I haven't got the chance to write a prototype though 🙁 I can't promise anything, but I do hope I'll do it. Having said that, I don't see sprintf getting any of that candy.

alexei avatar May 10 '17 16:05 alexei

Yes, I'm using the sprintf filter, with the addition of the number formatting that I mentioned.

Idea about a shiny new lib is definitely interesting! But yeah, adding some extensive features to sprintf is probably not feasible, I totally understand that.

patkec avatar May 10 '17 17:05 patkec

Just wondering where this has got to. I also have a need to format large numbers (not currency) and it would be handy to have this in sprintf

theophilusx avatar Sep 07 '18 01:09 theophilusx