question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Admit locales (es-ES, en-GB, etc.) to format the numbers

See original GitHub issue

Current behavior

Currently is necessary to describe how to format numbers and currencies by hand (except some languages), which makes it more involved on apps with a lot of languages because it’s necessary to provide the configuration for each of them.

Expected behavior

There is a native Intl.NumberFormat object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) that supports the locales.

Those locales work quite well, although they aren’t as customizable as autoNumeric, and it’s not possible to use cryptocurrencies or custom currencies. The biggest advantage is that they are just ready to use on all browsers without coding anything.

It could work like this:

new AutoNumeric(domElement, { locale: 'ja-JP' });

The locale configuration could later be overridden by any of the autoNumeric options.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ozumcommented, Nov 13, 2019

@AlexandreBonneau I submitted polished versions covering edge cases of above functions to npm and github. Please see intl-data source code.

You can use it or copy/paste it into your source code as you wish. I appreciate if you include my credits also.

0reactions
ozumcommented, Nov 8, 2019

@AlexandreBonneau please see above proposed solution. Also it could be a good idea to memoize values.

const localeOptionsCache = {};
const currencyCache = {};

/**
 * Returns group separators, decimal character, currency symbol placement, percent symbol and percent symbol placement for given locale.
 * @param {string} locale   - Locale to get options for.
 * @returns {Object}        - Group separators, decimal character, currency symbol, percent symbol and percent symbol placement.
 */
function getLocaleOptions(locale) {
  if (localeOptionsCache[locale] === undefined) {
    const [digitGroupSeparator, decimalCharacter] = (1111.1).toLocaleString(locale).replace(/1/g, "");
    const currencySymbolPlacement = (1).toLocaleString(locale, { style: "currency", currency: "USD" })[0] === "1" ? "s" : "p";
    localeOptionsCache[locale] = { digitGroupSeparator, decimalCharacter, currencySymbolPlacement };
    const percentString = (0.01).toLocaleString(locale, { style: "percent" });
    const percentSymbolPlacement = percentString[0] === "1" ? "s" : "p";
    const percentSymbol = percentString.replace("1", ""); // Some locales contains space, some not.
    localeOptionsCache[locale] = { digitGroupSeparator, decimalCharacter, currencySymbolPlacement, percentSymbolPlacement, percentSymbol };
  }
  return localeOptionsCache[locale];
}

/**
 * Returns currency symbol for given currency code.
 * @param {string} currencyCode - ISO currency code to get symbol for.
 * @returns {string}            - Currency symbol
 */
function getCurrencySign(currencyCode) {
  if (currencyCache[currencyCode] === undefined) {
    currencyCache[currencyCode] = (0).toLocaleString("tr-TR", { style: "currency", currency: currencyCode }).replace("0,00", "");
  }
  return currencyCache[currencyCode];
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Locales? · Issue #36 · iamkun/dayjs - GitHub
I mean perhaps some one want to return a string "4 years", another one want "4 years ago", and another one want something...
Read more >
Number.prototype.toLocaleString() - JavaScript | MDN
This tests for a global Intl object, checks that it's not null and that it has a NumberFormat property that is a function....
Read more >
Locale-based forms - Internationalization
This feature will be applied to submit, reset, number, date, datetime-local, month, time, week input types, and so on. This wiki page will...
Read more >
Language Subtag Registry - IANA
... Subtag: erw Description: Erokwanas Added: 2009-07-29 %% Type: language Subtag: ese Description: Ese Ejja Added: 2009-07-29 %% Type: language Subtag: esg ...
Read more >
Extensible Storage Engine (ESE) Database File (EDB) format ...
The Extensible Storage Engine (ESE) Database File (EDB) format is used by many Microsoft ... Change amount of in number of ... The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found