Parse Number compacted by `Intl.NumberFormat()`

Viewed 29

Is there an build-in function, that is able to parse a number formatted and compacted by

const formatter = new Intl.NumberFormat("en-US", {
    notation: "compact",
});
formatter.format(number);

There will obviously be some loss of information.

I was using this function for now, but I'm not sure, how robust it is.`

function parseNumberString(numberString) {
    const baseNumber = parseFloat(numberString.replace(localNumberSeparators\['group'\], '').replace(localNumberSeparators\['decimal'\], '.'));
    const parseScale = {
        'K': 3,
        'M': 6,
        'B': 9,
        'T': 12,
        'P': 15,
        'E': 18,
    }
    const scale = parseScale[numberString.slice(-1)] || 0;
    return baseNumber * Math.pow(10, scale);
}
0 Answers
Related