Proposal: Intl Locale Unformat

# Ryan King (5 years ago)

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.

# Steven R. Loomis (5 years ago)

There’s some discussion of parsing at tc39/ecma402#342 ( for Dates ) and tc39/ecma402#1 ( for Numbers - yes, issue #1 ).

One clear direction from the beginning (and painful experience) is to have formatting and parsing separate. Also, see the arguments against some uses of parsing that are better off as a date-picker.

,

-- Steven R. Loomis | @srl295 | git.io/srl295

# Ryan King (5 years ago)

Looks like everyone is on top of it. Thanks Steven!