UNPKG

15 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(.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 = format(".1f");
34for (var i = 0; i < 10; i++) {
35 console.log(f(.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
57format(".0%")(.123); // rounded percentage, "12%"
58format("($.2f")(-3.5); // localized fixed-point currency, "(£3.50)"
59format("+20")(42); // space-filled and signed, " +42"
60format(".^20")(42); // dot-filled and centered, ".........42........."
61format(".2s")(42e6); // SI-prefix with two significant digits, "42M"
62format("#x")(48879); // prefixed lowercase hexadecimal, "0xbeef"
63format(",.2r")(4223); // grouped thousands with two significant digits, "4,200"
64```
65
66See [*locale*.format](#locale_format) for a detailed specification, and try running [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).
71
72## API Reference
73
74<a name="format" href="#format">#</a> <b>format</b>(<i>specifier</i>)
75
76An alias for [*locale*.format](#locale_format) on the default [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or to define a new locale.
77
78<a name="formatPrefix" href="#formatPrefix">#</a> <b>formatPrefix</b>(<i>specifier</i>, <i>value</i>)
79
80An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the default [U.S. English](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js) locale. Use [localeFormat](#localeFormat) for a different built-in locale or to define a new locale.
81
82<a name="locale_format" href="#locale_format">#</a> <i>locale</i>.<b>format</b>(<i>specifier</i>)
83
84Returns 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:
85
86```
87[​[fill]align][sign][symbol][0][width][,][.precision][type]
88```
89
90The *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:
91
92* `>` - Forces the field to be right-aligned within the available space. (Default behavior).
93* `<` - Forces the field to be left-aligned within the available space.
94* `^` - Forces the field to be centered within the available space.
95* `=` - like `>`, but with any sign and symbol to the left of any padding.
96
97The *sign* can be:
98
99* `-` - nothing for positive and a minus sign for negative. (Default behavior.)
100* `+` - a plus sign for positive and a minus sign for negative.
101* `(` - nothing for positive and parentheses for negative.
102* ` ` (space) - a space for positive and a minus sign for negative.
103
104The *symbol* can be:
105
106* `$` - apply currency symbols per the locale definition.
107* `#` - for binary, octal, or hexadecimal notation, prefix by `0b`, `0o`, or `0x`, respectively.
108
109The *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.
110
111Depending 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.
112
113The available *type* values are:
114
115* `e` - exponent notation.
116* `f` - fixed point notation.
117* `g` - either decimal or exponent notation, rounded to significant digits.
118* `r` - decimal notation, rounded to significant digits.
119* `s` - decimal notation with an [SI prefix](#locale_formatPrefix), rounded to significant digits.
120* `%` - multiply by 100, and then decimal notation with a percent sign.
121* `p` - multiply by 100, round to significant digits, and then decimal notation with a percent sign.
122* `b` - binary notation, rounded to integer.
123* `o` - octal notation, rounded to integer.
124* `d` - decimal notation, rounded to integer.
125* `x` - hexadecimal notation, using lower-case letters, rounded to integer.
126* `X` - hexadecimal notation, using upper-case letters, rounded to integer.
127* `c` - converts the integer to the corresponding unicode character before printing.
128* `​` (none) - like `g`, but trim insignificant trailing zeros.
129
130The type `n` is also supported as 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:
131
132```js
133format(".2")(42); // "42"
134format(".2")(4.2); // "4.2"
135format(".1")(42); // "4e+1"
136format(".1")(4.2); // "4"
137```
138
139<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>)
140
141Equivalent 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:
142
143* `y` - yocto, 10⁻²⁴
144* `z` - zepto, 10⁻²¹
145* `a` - atto, 10⁻¹⁸
146* `f` - femto, 10⁻¹⁵
147* `p` - pico, 10⁻¹²
148* `n` - nano, 10⁻⁹
149* `µ` - micro, 10⁻⁶
150* `m` - milli, 10⁻³
151* `​` (none) - 10⁰
152* `k` - kilo, 10³
153* `M` - mega, 10⁶
154* `G` - giga, 10⁹
155* `T` - tera, 10¹²
156* `P` - peta, 10¹⁵
157* `E` - exa, 10¹⁸
158* `Z` - zetta, 10²¹
159* `Y` - yotta, 10²⁴
160
161Unlike [*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:
162
163```js
164var f = formatPrefix(",.0", 1e-6);
165f(.00042); // "420µ"
166f(.0042); // "4,200µ"
167```
168
169This 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.
170
171<a name="localeFormat" href="#localeFormat">#</a> <b>localeFormat</b>(<i>definition</i>)
172
173Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. If *definition* is a string, it is the name of a built-in locale:
174
175* `"ca-ES"` - [Catalan (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/ca-ES.js)
176* `"de-CH"` - [German (Switzerland)](https://github.com/d3/d3-format/tree/master/src/locale/de-CH.js)
177* `"de-DE"` - [German (Germany)](https://github.com/d3/d3-format/tree/master/src/locale/de-DE.js)
178* `"en-CA"` - [English (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/en-CA.js)
179* `"en-GB"` - [English (United Kingdom)](https://github.com/d3/d3-format/tree/master/src/locale/en-GB.js)
180* `"en-US"` - [English (United States)](https://github.com/d3/d3-format/tree/master/src/locale/en-US.js)
181* `"es-ES"` - [Spanish (Spain)](https://github.com/d3/d3-format/tree/master/src/locale/es-ES.js)
182* `"fi-FI"` - [Finnish (Finland)](https://github.com/d3/d3-format/tree/master/src/locale/fi-FI.js)
183* `"fr-CA"` - [French (Canada)](https://github.com/d3/d3-format/tree/master/src/locale/fr-CA.js)
184* `"fr-FR"` - [French (France)](https://github.com/d3/d3-format/tree/master/src/locale/fr-FR.js)
185* `"he-IL"` - [Hebrew (Israel)](https://github.com/d3/d3-format/tree/master/src/locale/he-IL.js)
186* `"hu-HU"` - [Hungarian (Hungary)](https://github.com/d3/d3-format/tree/master/src/locale/hu-HU.js)
187* `"it-IT"` - [Italian (Italy)](https://github.com/d3/d3-format/tree/master/src/locale/it-IT.js)
188* `"ja-JP"` - [Japanese (Japan)](https://github.com/d3/d3-format/tree/master/src/locale/ja-JP.js)
189* `"ko-KR"` - [Korean (South Korea)](https://github.com/d3/d3-format/tree/master/src/locale/ko-KR.js)
190* `"mk-MK"` - [Macedonian (Macedonia)](https://github.com/d3/d3-format/tree/master/src/locale/mk-MK.js)
191* `"nl-NL"` - [Dutch (Netherlands)](https://github.com/d3/d3-format/tree/master/src/locale/nl-NL.js)
192* `"pl-PL"` - [Polish (Poland)](https://github.com/d3/d3-format/tree/master/src/locale/pl-PL.js)
193* `"pt-BR"` - [Portuguese (Brazil)](https://github.com/d3/d3-format/tree/master/src/locale/pt-BR.js)
194* `"ru-RU"` - [Russian (Russia)](https://github.com/d3/d3-format/tree/master/src/locale/ru-RU.js)
195* `"sv-SE"` - [Swedish (Sweden)](https://github.com/d3/d3-format/tree/master/src/locale/sv-SE.js)
196* `"zh-CN"` - [Chinese (China)](https://github.com/d3/d3-format/tree/master/src/locale/zh-CN.js)
197
198Otherwise, the locale *definition* must include the following properties:
199
200* `decimal` - the decimal point (e.g., `"."`).
201* `thousands` - the group separator (e.g., `","`).
202* `grouping` - the array of group sizes (e.g., `[3]`), cycled as needed.
203* `currency` - the currency prefix and suffix (e.g., `["$", ""]`).
204
205Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.
206
207<a name="formatSpecifier" href="#formatSpecifier">#</a> <b>formatSpecifier</b>(<i>specifier</i>)
208
209Parses 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:
210
211```js
212{
213 "fill": " ",
214 "align": ">",
215 "sign": "-",
216 "symbol": "",
217 "zero": false,
218 "width": undefined,
219 "comma": false,
220 "precision": 6,
221 "type": "s"
222}
223```
224
225This 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:
226
227```js
228var s = formatSpecifier("f");
229s.precision = precisionFixed(.01);
230var f = format(s);
231f(42); // "42.00";
232```
233
234<a name="precisionFixed" href="#precisionFixed">#</a> <b>precisionFixed</b>(<i>step</i>)
235
236Returns 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:
237
238```js
239var p = precisionFixed(0.5),
240 f = format("." + p + "f");
241f(1); // "1.0"
242f(1.5); // "1.5"
243f(2); // "2.0"
244```
245
246Whereas for the numbers 1, 2 and 3, the *step* should be 1 and the suggested precision is 0:
247
248```js
249var p = precisionFixed(1),
250 f = format("." + p + "f");
251f(1); // "1"
252f(2); // "2"
253f(3); // "3"
254```
255
256Note: for the `%` format type, subtract two:
257
258```js
259var p = Math.max(0, precisionFixed(0.05) - 2),
260 f = format("." + p + "%");
261f(.45); // "45%"
262f(.50); // "50%"
263f(.55); // "55%"
264```
265
266<a name="precisionPrefix" href="#precisionPrefix">#</a> <b>precisionPrefix</b>(<i>step</i>, <i>value</i>)
267
268Returns 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:
269
270```js
271var p = precisionPrefix(1e5, 1.3e6),
272 f = formatPrefix("." + p, 1.3e6);
273f(1.1e6); // "1.1M"
274f(1.2e6); // "1.2M"
275f(1.3e6); // "1.3M"
276```
277
278<a name="precisionRound" href="#precisionRound">#</a> <b>precisionRound</b>(<i>step</i>, <i>max</i>)
279
280Returns 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:
281
282```js
283var p = precisionRound(0.01, 1.01),
284 f = format("." + p + "r");
285f(0.99); // "0.990"
286f(1.0); // "1.00"
287f(1.01); // "1.01"
288```
289
290Whereas 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:
291
292```js
293var p = precisionRound(0.1, 1.1),
294 f = format("." + p + "r");
295f(0.9); // "0.90"
296f(1.0); // "1.0"
297f(1.1); // "1.1"
298```
299
300Note: for the `e` format type, subtract one:
301
302```js
303var p = Math.max(0, precisionRound(0.01, 1.01) - 1),
304 f = format("." + p + "e");
305f(0.01); // "1.00e-2"
306f(1.01); // "1.01e+0"
307```