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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more