Proposal: Intl Locale Unformat

Ryan King ryan.king1809 at gmail.com
Tue Jun 25 22:41:16 UTC 2019


Would it be possible to implement an unformat option for Intl Locale?

When a user is inputting very large numbers it's nice to allow them to
format the number (or have it auto-formatted) so they can read what they
are typing. It would be great to be able to convert it back to a number /
date / etc. based on their locale.

Something like:
```
Intl.NumberFormat(locale).unformat(string)
```

I currently do it myself for numbers by creating an unformatter function
using the `decimal` object in `formatToParts()`
```
import _ from "lodash"

const locale = window.navigator.userLanguage || window.navigator.language;
const numberFormatter = new Intl.NumberFormat(locale)
const numberDecimalString = _.find(numberFormatter.formatToParts(0.1),
{type: "decimal"}).value
const stripNonNumeric = string => string.replace(/\D/g,'');
const splitStringToFloat = splitString => {
const intString = stripNonNumeric(splitString[0])
const decmalString = stripNonNumeric(splitString[1])
return parseFloat(`${intString}.${decmalString}`)
}
const numberUnformatter = string => {
const splitString = string.split(numberDecimalString)
return splitStringToFloat(splitString)
}
```

It would also be great to be able to do the same for dates and other Intl
formats.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190625/2450a458/attachment.html>


More information about the es-discuss mailing list