Public
Edited
Oct 13, 2023
Fork of d3.format
1 star
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(",.3r")(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
Insert cell
d3 = require("d3-format@3")
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