Formatting Cheat Sheet
Number Formatting
Fixed Decimal Format (
f
):Example: Formatting
12345.6789
to two decimal places.D3 Format:
d3.format(".2f")
Output:
"12345.68"
Percentage Format (
%
):Example: Formatting
0.123
as a percentage.D3 Format:
d3.format(".1%")
Output:
"12.3%"
Scientific Notation (
e
):Example: Formatting
12345
in scientific notation.D3 Format:
d3.format(".2e")
Output:
"1.23e+4"
SI Prefix with Significant Digits (
s
):Example: Formatting
12345
with SI prefix.D3 Format:
d3.format(".3s")
Output:
"12.3k"
Rounded (
r
):Example: Rounding
12345.6789
.D3 Format:
d3.format(".4r")
Output:
"12350"
Comma Separated (
,
):Example: Formatting
12345.6789
with comma separation.D3 Format:
d3.format(",.2f")
Output:
"12,345.68"
Currency (
$
):Example: Formatting
12345.67
as currency.D3 Format:
d3.format("$,.2f")
Output:
"$12,345.67"
Binary (
b
):Example: Formatting
10
in binary.D3 Format:
d3.format("b")
Output:
"1010"
Hexadecimal (
x
orX
):Example: Formatting
255
in hexadecimal.D3 Format:
d3.format("x")
Output:
"ff"
Zero Padding (
0
):Example: Formatting
5
as a two-digit number.D3 Format:
d3.format("02d")
Output:
"05"
These examples show how D3.js can format numbers in many ways, including fixed decimals, percentages, scientific notation, and more. The format specifiers (like .2f
, .1%
, .3s
) determine the precise formatting method.
For more detailed information and examples, you can refer to the D3.js Format API Reference.
Time Formatting
Abbreviated Weekday Name (
%a
):Example: Formatting a date to show the abbreviated weekday.
D3 Format:
d3.timeFormat("%a")
Output for a Sunday:
"Sun"
Full Weekday Name (
%A
):Example: Formatting a date to show the full weekday name.
D3 Format:
d3.timeFormat("%A")
Output for a Sunday:
"Sunday"
Abbreviated Month Name (
%b
):Example: Formatting a date to show the abbreviated month name.
D3 Format:
d3.timeFormat("%b")
Output for January:
"Jan"
Full Month Name (
%B
):Example: Formatting a date to show the full month name.
D3 Format:
d3.timeFormat("%B")
Output for January:
"January"
Zero-Padded Day of the Month (
%d
):Example: Formatting a date to show the day of the month with zero padding.
D3 Format:
d3.timeFormat("%d")
Output for the 5th of the month:
"05"
24-Hour Clock Hour (
%H
):Example: Formatting a time to show the hour in 24-hour format.
D3 Format:
d3.timeFormat("%H")
Output for 3 PM:
"15"
12-Hour Clock Hour (
%I
):Example: Formatting a time to show the hour in 12-hour format.
D3 Format:
d3.timeFormat("%I")
Output for 15:00 (3 PM):
"03"
Month as Decimal Number (
%m
):Example: Formatting a date to show the month as a decimal number.
D3 Format:
d3.timeFormat("%m")
Output for January:
"01"
Minute as Decimal Number (
%M
):Example: Formatting a time to show the minute.
D3 Format:
d3.timeFormat("%M")
Output for 07 minutes past:
"07"
AM or PM (
%p
):Example: Formatting a time to indicate AM or PM.
D3 Format:
d3.timeFormat("%p")
Output for 3 PM:
"PM"
Second as Decimal Number (
%S
):Example: Formatting a time to show the second.
D3 Format:
d3.timeFormat("%S")
Output for 09 seconds past:
"09"
Year without Century (
%y
):Example: Formatting a date to show the year without the century.
D3 Format:
d3.timeFormat("%y")
Output for 2024:
"24"
Year with Century (
%Y
):Example: Formatting a date to show the full year.
D3 Format:
d3.timeFormat("%Y")
Output for 2024:
"2024"
These examples demonstrate how to format dates and times in D3.js using different directives. By combining these directives, you can create custom formats for any date or time value. For more detailed information and examples, refer to the D3.js Time Format API Reference.
Last updated
Was this helpful?