A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

[Proposal] Intl.NumberFormat is incomplete and needs a respective Parser

goatandsheep
2018-01-03

The internationalization API is a very simple way to convert numbers to a string format including comma/period placement and even currencies around the world. However, there is no way to reverse that API. This renders the Intl.numberFormat function fairly unusable, especially when converting back and forth between an integer, server format and a string, display format for input fields.

This can be solved using a number parser that involves the i18n info, such as locale and/or currency code. We could use the format patterns already associated with the locales, currencies, etc. to match floats in the given number string.

As this is my first post, I am looking for feedback on:

  • How do I obtain the number formats baked into the spec?
  • What additional information / investigations do I need to provide to make this proposal?
goatandsheep
2018-01-05

I wrote up a generic example, but I don’t think this will work for all locales:

/**
 * @param {Array} locale Would be used to find the regex associated with the locale to be filtered out of the price 
 * @param {String} price
 * @returns {Number}
 */
function NumberParse(locale, price) {
  let reSym = /\d|\.|\,|\-/gi;
  let numArray = price.match(reSym);
  let initSweep numArray.join('')
  initSweep = initSweep.replace(/\,/g, '.')
  if (initSweep[0] === '-') {
    initSweep = initSweep.replace(/\-/g, '')
    initSweep = '-'.concat(initSweep)
  }
  else {
    initSweep = initSweep.replace(/\-/g, '')
  }
  numArray = initSweep.split('.')
  let tempDec = '00'
  if (numArray.length > 1) {
    tempDec = numArray.pop();
  }
  initSweep = numArray.join('')
  initSweep = initSweep.concat('.', tempDec)
  return parseFloat(initSweep)
}
goatandsheep
2018-01-08

The above code doesn’t work for currencies without decimal denominations, but I hope you understand it’s for demonstration purposes only and this could be resolved if I could use the locale information :wink:.

goatandsheep
2018-01-10

This handles negatives as well. If someone puts “-69.900,00 US-Dollar” it’ll be “-69900.00” after the initial sweep.

goatandsheep
2018-01-16

Anyone have suggestions on how to format the proposal?

goatandsheep
2018-01-18

What are the next steps for the proposal? @domenic