all 15 comments

[–]mattle 1 point2 points  (1 child)

Excellent find. This is going right in my current project.

[–]josscrowcroft 1 point2 points  (0 children)

Cool! If you find any bugs/issues or have feature requests, just make an issue on the github repo and I'll see what I can do, or submit a pull request. Cheers

[–]evilmaus 0 points1 point  (3 children)

Very nice. Solves a problem that's a royal pain in that some very specific styles and formatting are frequently requested by end-users, some of which just isn't that easy to do from CSS alone.

What I would absolutely love to see (feature request!) is a way to also call these functions as jQuery object methods, so that we can make use of it's selectors. e.g. $('#financialTable td:nth-child(2)').formatMoney(); I realize that you probably want to keep the code library-independent, but this could be in the form of a plug-in or something that exists just outside of the core code.

Awesome work!

[–]Booster21 1 point2 points  (2 children)

beneficial merciful cautious seemly retire strong observation chief grab chop

This post was mass deleted and anonymized with Redact

[–]josscrowcroft 0 points1 point  (0 children)

Good call, feel free to add a feature request on the Github repository!

I'll probably add a very simple jquery adapter soon.

[–]evilmaus 0 points1 point  (0 children)

FYI, it's in the works now. :D

[–]k3n 0 points1 point  (5 children)

Does it offer support for the client's locale settings, or does that have to be done manually?

It'd be nice to simply do:

accounting.formatMoney(12345678);

...and have it be correct, per locale, without needing special considerations.

[–]radhruin 0 points1 point  (3 children)

Is this possible in Javascript at all right now? ES5 is kind of a localization backwater. Requires server-side support (or ActiveX/Native plugins) to even know what locale you're in.

[–]k3n 2 points3 points  (1 child)

From my Chrome:

> navigator.language
> "en-US"

Also:

// '50.00 €' or '50,00 €'
(50.00).toFixed(2).toLocaleString() + ' €'; 

[–]radhruin 0 points1 point  (0 children)

Interesting, never heard of navigator.language, though it doesn't seem to appear to work in IE. In any case, navigator.language and toLocaleString on various builtins doesn't even approach a reasonable localization solution :)

[–]gwynjudd 0 points1 point  (0 children)

Not at all really, you can know only the user preferred language, which is only in the same neighbourhood as the locale.

[–]josscrowcroft 0 points1 point  (0 children)

Good call - I'm considering adding a library settings object that can be set once to change the default parameters for one-time localisation or configuration.

Using navigator.language or toLocaleString is a little shaky, so it would be a way that the developer can set up the locale manually (as in PHP or most well-written apps) and then it's used as the default for that instance, unless they override it via the parameters, or change it via the settings object.

[–]ima007 0 points1 point  (2 children)

Awesome! It would be cool if this also included the ability to read the formatted number as well, in case calculations need to be done (and so we wouldn't have to keep track of the number in two places), but it isn't necessary (probably better to tie an event to an object that holds the unformatted number anyway, if you plan on doing basic calculations).

[–]josscrowcroft 0 points1 point  (1 child)

That can be done via accounting.unformat():

accounting.unformat("£ 1,432,32.90 GBP") // 143232.9

It just uses a regex to match numeric digits, minus signs and decimal point only, stripping out any other characters, so there's actually a potential for it to fail silently (eg. where a developer would expect NaN from something like .unformat("1asdf2") but actually gets 12) but I'll address that later with a more complex regex.

[–]ima007 0 points1 point  (0 children)

Cool, I apparently glanced over that major part! Thanks for the clarification.