Published
Edited
Mar 24, 2019
1 fork
Importers
16 stars
Insert cell
Insert cell
new Intl.NumberFormat("en").format(12345678.90)
Insert cell
new Intl.NumberFormat("de").format(12345678.90)
Insert cell
new Intl.NumberFormat("en-IN").format(12345678.90)
Insert cell
Insert cell
parts = new Intl.NumberFormat("de").formatToParts(1234.5)
Insert cell
group = parts.find(d => d.type === "group").value
Insert cell
decimal = parts.find(d => d.type === "decimal").value
Insert cell
Insert cell
"12.345.678,9".replace(/\./g, "").replace(/,/, ".")
Insert cell
Insert cell
+"12345678.9"
Insert cell
Insert cell
[...new Intl.NumberFormat("ar-EG", {useGrouping: false}).format(9876543210)].reverse()
Insert cell
[...new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec", {useGrouping: false}).format(9876543210)].reverse()
Insert cell
Insert cell
class NumberParser {
constructor(locale) {
const parts = new Intl.NumberFormat(locale).formatToParts(12345.6);
const numerals = [...new Intl.NumberFormat(locale, {useGrouping: false}).format(9876543210)].reverse();
const index = new Map(numerals.map((d, i) => [d, i]));
this._group = new RegExp(`[${parts.find(d => d.type === "group").value}]`, "g");
this._decimal = new RegExp(`[${parts.find(d => d.type === "decimal").value}]`);
this._numeral = new RegExp(`[${numerals.join("")}]`, "g");
this._index = d => index.get(d);
}
parse(string) {
return (string = string.trim()
.replace(this._group, "")
.replace(this._decimal, ".")
.replace(this._numeral, this._index)) ? +string : NaN;
}
}
Insert cell
Insert cell
new NumberParser("ar-EG").parse("١٬٢٣٤٫٥٦")
Insert cell
new NumberParser("zh-Hans-CN-u-nu-hanidec").parse("一,二三四.五六")
Insert cell
new NumberParser("en").parse("12,345,678.90")
Insert cell
new NumberParser("de").parse("12.345.678,9")
Insert cell
new NumberParser("en-IN").parse("1,23,45,678.9")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more