UNPKG

15.2 kBMarkdownView Raw
1# d3-format
2
3Ever noticed how sometimes JavaScript doesn’t display numbers the way you expect? Like, you tried to print tenths with a simple loop:
4
5```js
6for (var i = 0; i < 10; i++) {
7 console.log(0.1 * i);
8}
9```
10
11And you got this:
12
13```js
140
150.1
160.2
170.30000000000000004
180.4
190.5
200.6000000000000001
210.7000000000000001
220.8
230.9
24```
25
26Welcome to [binary floating point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)! ಠ_ಠ
27
28Yet rounding error is not the only reason to customize number formatting. A table of numbers should be formatted consistently for comparison; above, 0.0 would be better than 0. Large numbers should have grouped digits (e.g., 42,000) or be in scientific or metric notation (4.2e+4, 42k). Currencies should have fixed precision ($3.50). Reported numerical results should be rounded to significant digits (4021 becomes 4000). Number formats should appropriate to the reader’s locale (42.000,00 or 42,000.00). The list goes on.
29
30Formatting numbers for human consumption is the purpose of d3-format, which is modeled after Python 3’s [format specification mini-language](https://docs.python.org/3/library/string.html#format-specification-mini-language) ([PEP 3101](https://www.python.org/dev/peps/pep-3101/)). Revisiting the example above:
31
32```js
33var f = d3.format(".1f");
34for (var i = 0; i < 10; i++) {
35 console.log(f(0.1 * i));
36}
37```
38
39Now you get this:
40
41```js
420.0
430.1
440.2
450.3
460.4
470.5
480.6
490.7
500.8
510.9
52```
53
54But d3-format is much more than an alias for [number.toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)! A few more examples:
55
56```js
57d3.format(".0%")(0.123); // rounded percentage, "12%"
58d3.format("($.2f")(-3.5); // localized fixed-point currency, "(£3.50)"
59d3.format("+20")(42); // space-filled and signed, " +42"
60d3.format(".^20")(42); // dot-filled and centered, ".........42........."
61d3.format(".2s")(42e6); // SI-prefix with two significant digits, "42M"
62d3.format("#x")(48879); // prefixed lowercase hexadecimal, "0xbeef"
63d3.format(",.2r")(4223); // grouped thousands with two significant digits, "4,200"
64```
65
66See [*locale*.format](#locale_format) for a detailed specification, and try running [d3.formatSpecifier](#formatSpecifier) on the above formats to decode their meaning.
67
68## Installing
69
70If you use NPM, `npm install d3-format`. Otherwise, download the [latest release](https://github.com/d3/d3-format/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-format.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
71
72```html
73<script src="https://d3js.org/d3-format.v1.min.js"></script>
74<script>
75
76var format = d3.format(".2s");
77
78</script>
79```
80
81Locale files are hosted on [unpkg](https://unpkg.com/) and can be loaded using [d3.json](https://github.com/d3/d3-request/blob/master/README.md#json). For example, to set Russian as the default locale:
82
83```js
84d3.json("https://unpkg.com/d3-format@1/locale/ru-RU.json", function(error, locale) {
85 if (error) throw error;
86
87 d3.formatDefaultLocale(locale);
88
89 var format = d3.format("$,");
90
91 console.log(format(1234.56)); // 1 234,56 руб.
92});
93```
94
95[Try d3-format in your browser.](https://tonicdev.com/npm/d3-format)
96
97## API Reference
98
99<a name="format" href="#format">#</a> d3.<b>format</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js#L4 "Source")
100
101An alias for [*locale*.format](#locale_format) on the [default locale](#formatDefaultLocale).
102
103<a name="formatPrefix" href="#formatPrefix">#</a> d3.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js#L5 "Source")
104
105An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the [default locale](#formatDefaultLocale).
106
107<a name="locale_format" href="#locale_format">#</a> <i>locale</i>.<b>format</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js#L18 "Source")
108
109Returns a new format function for the given string *specifier*. The returned function takes a number as the only argument, and returns a string representing the formatted number. The general form of a specifier is:
110
111```
112[​[fill]align][sign][symbol][0][width][,][.precision][~][type]
113```
114
115The *fill* can be any character. The presence of a fill character is signaled by the *align* character following it, which must be one of the following:
116
117* `>` - Forces the field to be right-aligned within the available space. (Default behavior).
118* `<` - Forces the field to be left-aligned within the available space.
119* `^` - Forces the field to be centered within the available space.
120* `=` - like `>`, but with any sign and symbol to the left of any padding.
121
122The *sign* can be:
123
124* `-` - nothing for zero or positive and a minus sign for negative. (Default behavior.)
125* `+` - a plus sign for zero or positive and a minus sign for negative.
126* `(` - nothing for zero or positive and parentheses for negative.
127* ` ` (space) - a space for zero or positive and a minus sign for negative.
128
129The *symbol* can be:
130
131* `$` - apply currency symbols per the locale definition.
132* `#` - for binary, octal, or hexadecimal notation, prefix by `0b`, `0o`, or `0x`, respectively.
133
134The *zero* (`0`) option enables zero-padding; this implicitly sets *fill* to `0` and *align* to `=`. The *width* defines the minimum field width; if not specified, then the width will be determined by the content. The *comma* (`,`) option enables the use of a group separator, such as a comma for thousands.
135
136Depending on the *type*, the *precision* either indicates the number of digits that follow the decimal point (types `f` and `%`), or the number of significant digits (types `​`, `e`, `g`, `r`, `s` and `p`). If the precision is not specified, it defaults to 6 for all types except `​` (none), which defaults to 12. Precision is ignored for integer formats (types `b`, `o`, `d`, `x`, `X` and `c`). See [precisionFixed](#precisionFixed) and [precisionRound](#precisionRound) for help picking an appropriate precision.
137
138The `~` option trims insignificant trailing zeros across all format types. This is most commonly used in conjunction with types `r`, `e`, `s` and `%`. For example:
139
140```js
141d3.format("s")(1500); // "1.50000k"
142d3.format("~s")(1500); // "1.5k"
143```
144
145The available *type* values are:
146
147* `e` - exponent notation.
148* `f` - fixed point notation.
149* `g` - either decimal or exponent notation, rounded to significant digits.
150* `r` - decimal notation, rounded to significant digits.
151* `s` - decimal notation with an [SI prefix](#locale_formatPrefix), rounded to significant digits.
152* `%` - multiply by 100, and then decimal notation with a percent sign.
153* `p` - multiply by 100, round to significant digits, and then decimal notation with a percent sign.
154* `b` - binary notation, rounded to integer.
155* `o` - octal notation, rounded to integer.
156* `d` - decimal notation, rounded to integer.
157* `x` - hexadecimal notation, using lower-case letters, rounded to integer.
158* `X` - hexadecimal notation, using upper-case letters, rounded to integer.
159* `c` - converts the integer to the corresponding unicode character before printing.
160
161The type `​` (none) is also supported as shorthand for `~g` (with a default precision of 12 instead of 6), and the type `n` is shorthand for `,g`. For the `g`, `n` and `​` (none) types, decimal notation is used if the resulting string would have *precision* or fewer digits; otherwise, exponent notation is used. For example:
162
163```js
164d3.format(".2")(42); // "42"
165d3.format(".2")(4.2); // "4.2"
166d3.format(".1")(42); // "4e+1"
167d3.format(".1")(4.2); // "4"
168```
169
170<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js#L127 "Source")
171
172Equivalent to [*locale*.format](#locale_format), except the returned function will convert values to the units of the appropriate [SI prefix](https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes) for the specified numeric reference *value* before formatting in fixed point notation. The following prefixes are supported:
173
174* `y` - yocto, 10⁻²⁴
175* `z` - zepto, 10⁻²¹
176* `a` - atto, 10⁻¹⁸
177* `f` - femto, 10⁻¹⁵
178* `p` - pico, 10⁻¹²
179* `n` - nano, 10⁻⁹
180* `µ` - micro, 10⁻⁶
181* `m` - milli, 10⁻³
182* `​` (none) - 10⁰
183* `k` - kilo, 10³
184* `M` - mega, 10⁶
185* `G` - giga, 10⁹
186* `T` - tera, 10¹²
187* `P` - peta, 10¹⁵
188* `E` - exa, 10¹⁸
189* `Z` - zetta, 10²¹
190* `Y` - yotta, 10²⁴
191
192Unlike [*locale*.format](#locale_format) with the `s` format type, this method returns a formatter with a consistent SI prefix, rather than computing the prefix dynamically for each number. In addition, the *precision* for the given *specifier* represents the number of digits past the decimal point (as with `f` fixed point notation), not the number of significant digits. For example:
193
194```js
195var f = d3.formatPrefix(",.0", 1e-6);
196f(0.00042); // "420µ"
197f(0.0042); // "4,200µ"
198```
199
200This method is useful when formatting multiple numbers in the same units for easy comparison. See [precisionPrefix](#precisionPrefix) for help picking an appropriate precision, and [bl.ocks.org/9764126](http://bl.ocks.org/mbostock/9764126) for an example.
201
202<a name="formatSpecifier" href="#formatSpecifier">#</a> d3.<b>formatSpecifier</b>(<i>specifier</i>) [<>](https://github.com/d3/d3-format/blob/master/src/formatSpecifier.js "Source")
203
204Parses the specified *specifier*, returning an object with exposed fields that correspond to the [format specification mini-language](#locale_format) and a toString method that reconstructs the specifier. For example, `formatSpecifier("s")` returns:
205
206```js
207{
208 "fill": " ",
209 "align": ">",
210 "sign": "-",
211 "symbol": "",
212 "zero": false,
213 "width": undefined,
214 "comma": false,
215 "precision": undefined,
216 "trim": false,
217 "type": "s"
218}
219```
220
221This method is useful for understanding how format specifiers are parsed and for deriving new specifiers. For example, you might compute an appropriate precision based on the numbers you want to format using [precisionFixed](#precisionFixed) and then create a new format:
222
223```js
224var s = d3.formatSpecifier("f");
225s.precision = d3.precisionFixed(0.01);
226var f = d3.format(s);
227f(42); // "42.00";
228```
229
230<a name="precisionFixed" href="#precisionFixed">#</a> d3.<b>precisionFixed</b>(<i>step</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionFixed.js "Source")
231
232Returns a suggested decimal precision for fixed point notation given the specified numeric *step* value. The *step* represents the minimum absolute difference between values that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1, 1.5, and 2, the *step* should be 0.5 and the suggested precision is 1:
233
234```js
235var p = d3.precisionFixed(0.5),
236 f = d3.format("." + p + "f");
237f(1); // "1.0"
238f(1.5); // "1.5"
239f(2); // "2.0"
240```
241
242Whereas for the numbers 1, 2 and 3, the *step* should be 1 and the suggested precision is 0:
243
244```js
245var p = d3.precisionFixed(1),
246 f = d3.format("." + p + "f");
247f(1); // "1"
248f(2); // "2"
249f(3); // "3"
250```
251
252Note: for the `%` format type, subtract two:
253
254```js
255var p = Math.max(0, d3.precisionFixed(0.05) - 2),
256 f = d3.format("." + p + "%");
257f(0.45); // "45%"
258f(0.50); // "50%"
259f(0.55); // "55%"
260```
261
262<a name="precisionPrefix" href="#precisionPrefix">#</a> d3.<b>precisionPrefix</b>(<i>step</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionPrefix.js "Source")
263
264Returns a suggested decimal precision for use with [*locale*.formatPrefix](#locale_formatPrefix) given the specified numeric *step* and reference *value*. The *step* represents the minimum absolute difference between values that will be formatted, and *value* determines which SI prefix will be used. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1.1e6, 1.2e6, and 1.3e6, the *step* should be 1e5, the *value* could be 1.3e6, and the suggested precision is 1:
265
266```js
267var p = d3.precisionPrefix(1e5, 1.3e6),
268 f = d3.formatPrefix("." + p, 1.3e6);
269f(1.1e6); // "1.1M"
270f(1.2e6); // "1.2M"
271f(1.3e6); // "1.3M"
272```
273
274<a name="precisionRound" href="#precisionRound">#</a> d3.<b>precisionRound</b>(<i>step</i>, <i>max</i>) [<>](https://github.com/d3/d3-format/blob/master/src/precisionRound.js "Source")
275
276Returns a suggested decimal precision for format types that round to significant digits given the specified numeric *step* and *max* values. The *step* represents the minimum absolute difference between values that will be formatted, and the *max* represents the largest absolute value that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 0.99, 1.0, and 1.01, the *step* should be 0.01, the *max* should be 1.01, and the suggested precision is 3:
277
278```js
279var p = d3.precisionRound(0.01, 1.01),
280 f = d3.format("." + p + "r");
281f(0.99); // "0.990"
282f(1.0); // "1.00"
283f(1.01); // "1.01"
284```
285
286Whereas for the numbers 0.9, 1.0, and 1.1, the *step* should be 0.1, the *max* should be 1.1, and the suggested precision is 2:
287
288```js
289var p = d3.precisionRound(0.1, 1.1),
290 f = d3.format("." + p + "r");
291f(0.9); // "0.90"
292f(1.0); // "1.0"
293f(1.1); // "1.1"
294```
295
296Note: for the `e` format type, subtract one:
297
298```js
299var p = Math.max(0, d3.precisionRound(0.01, 1.01) - 1),
300 f = d3.format("." + p + "e");
301f(0.01); // "1.00e-2"
302f(1.01); // "1.01e+0"
303```
304
305### Locales
306
307<a name="formatLocale" href="#formatLocale">#</a> d3.<b>formatLocale</b>(<i>definition</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js "Source")
308
309Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. The *definition* must include the following properties:
310
311* `decimal` - the decimal point (e.g., `"."`).
312* `thousands` - the group separator (e.g., `","`).
313* `grouping` - the array of group sizes (e.g., `[3]`), cycled as needed.
314* `currency` - the currency prefix and suffix (e.g., `["$", ""]`).
315* `numerals` - optional; an array of ten strings to replace the numerals 0-9.
316* `percent` - optional; the percent suffix (defaults to `"%"`).
317
318Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.
319
320<a name="formatDefaultLocale" href="#formatDefaultLocale">#</a> d3.<b>formatDefaultLocale</b>(<i>definition</i>) [<>](https://github.com/d3/d3-format/blob/master/src/defaultLocale.js "Source")
321
322Equivalent to [d3.formatLocale](#formatLocale), except it also redefines [d3.format](#format) and [d3.formatPrefix](#formatPrefix) to the new locale’s [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix). If you do not set a default locale, it defaults to [U.S. English](https://github.com/d3/d3-format/blob/master/locale/en-US.json).