openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.95k stars 532 forks source link

Global configuration for currency formatting ('Currency != Number format') #6

Closed lukebarton closed 13 years ago

lukebarton commented 13 years ago

Currency is not the same thing as number format and therefore I propose to you that they be treated seperately, hopefully using some sort of locale identifier.

Being from the UK, I prefer numbers in the following format: 123,456.78

Viewing a price in EUR € doesn't mean I would like the numbers displayed as 123.456,78

Zend Framework has an implementation you may find helpful to review: http://framework.zend.com/manual/en/zend.locale.parsing.html#zend.locale.number.localize

wjcrowcroft commented 13 years ago

Fair point - I'm from the UK as well, which is IMHO the only place in the world that does currency right.. I agree that a user shouldn't have to see EU-style formatting if they're used to standard formatting, or vice-versa..

Having said that, I think this is something that will be best handled by the parameters passed to the method (and in the next version, some form of configuration/settings object) - which leaves it squarely up to the developer. I'd say the job of the library is to provide a way of making that as easy as possible for them to present the correct number formatting for any given user.

It would definitely be an application-specific method of defining which locale to set, so I think this will be solved by the planned global settings configuration for the next version. Hope you can check it out when it's updated and let me know your thoughts!

Oh and thanks for the link, I'll take a look over it.

lukebarton commented 13 years ago

Currency is just a unit, just like weight, pressure, temperature, distance or density. The value (number) is seperate from that.

I agree about the parameter, a quick google suggests there is little you can garner from the client itself. Perhaps a global configuration overridable by a parameter would be the best approach.

wjcrowcroft commented 13 years ago

Yeah, good call. Working on that now.

lukebarton commented 13 years ago

Damn comment & close button :)

wjcrowcroft commented 13 years ago

Hey, I've just pushed v0.1.3 which adds a configurable settings object, which allows a developer to set the global localisation defaults under two objects, currency and number:

// Settings object that controls default parameters for library methods:
accounting.settings = {
    currency: {
        symbol : "$",   // default currency symbol is '$'
        format: "%s%v", // this controls string output: %s = symbol, %v = value/number
        decimal : ".",  // decimal point separator
        thousand: ",",  // thousands separator
        precision : 2   // decimal places
    },
    number: {
        precision : 0,  // default precision on numbers is 0
        thousand: ",",
        decimal : "."
    }
}

I'm leaving it up to individual developers and applications to decide which formatting to show the user, but I do have plans to build an optional plugin with default locale data for some common regions or countries... more on that later.

Until then I'd say this one's wrapped up, though there's more to do fo' sho'

Please re-open or make a new issue if you find bugs in the new code. Cheers!