Public
Edited
Oct 26, 2023
9 forks
44 stars
Insert cell
Insert cell
d3.format(".1f")(0.1 + 0.2) // fixed decimal
Insert cell
d3.format(".0%")(0.123) // rounded percentage
Insert cell
d3.format("($.2f")(-3.5) // localized fixed-point currency
Insert cell
d3.format("+20")(42) // space-filled and signed
Insert cell
d3.format(".^20")(42) // dot-filled and centered
Insert cell
d3.format(".2s")(42e6) // SI-prefix with two significant digits
Insert cell
d3.format("#x")(48879) // prefixed lowercase hexadecimal
Insert cell
d3.format(",.2r")(4223) // grouped thousands with two significant digits
Insert cell
Insert cell
d3.format("s")(1500)
Insert cell
d3.format("~s")(-1500)
Insert cell
Insert cell
d3.format(".2")(42)
Insert cell
d3.format(".2")(4.2)
Insert cell
d3.format(".1")(42)
Insert cell
d3.format(".1")(4.2)
Insert cell
Insert cell
f = d3.formatPrefix(",.0", 1e-6)
Insert cell
f(0.00042)
Insert cell
f(0.0042)
Insert cell
Insert cell
s = Object.assign(d3.formatSpecifier("f"), {precision: d3.precisionFixed(0.01)})
Insert cell
d3.format(s)(42)
Insert cell
Insert cell
new d3.FormatSpecifier({type: "s"})
Insert cell
Insert cell
Insert cell
Insert cell
locale = d3.formatLocale({
decimal: ",",
thousands: "\u00a0",
grouping: [3],
currency: ["", "\u00a0€"],
minus: "\u2212",
percent: "\u202f%"
})
Insert cell
fformat = locale.format("$,")
Insert cell
fformat(123456.25)
Insert cell
Insert cell
{
const localeEnIN = await fetch("https://cdn.jsdelivr.net/npm/d3-format@1/locale/en-IN.json")
.then(d => d.json())
.then(d3.formatLocale);
const format = localeEnIN.format("$,");
return format(123456.25);
}
Insert cell
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