Platform
Resources
Pricing
Sign in
Get started
D3
Bring your data to life.
Workspace
Fork
Public
d3-format
By
Mike Bostock
Edited
Oct 26, 2023
ISC
10 forks
47 stars
d3-format
d3.format
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
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.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
f
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
s
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
locale
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
fformat
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML