API Reference ▸ Core ▸ Formatting
Formatting numbers is one of those things you don't normally think about until an ugly "0.30000000000000004" appears on your axis labels. Also, maybe you want to group thousands to improve readability, and use fixed precision, such as "$1,240.10". Or, maybe you want to display only the significant digits of a particular number. D3 makes this easy using a standard number format. For example, to create a function that zero-fills to four digits, say:
var zero = d3.format("04d");
Now you can conveniently format numbers:
zero(2); // "0002" zero(123); // "0123"
In addition to numbers, D3 also supports formatting and parsing dates, and comma-separated values.
Returns a new format function with the given string specifier. (Equivalent to locale.numberFormat for the default U.S. English locale.) A format function takes a number as the only argument, and returns a string representing the formatted number. The format specifier is modeled after Python 3.1's built-in format specification mini-language. The general form of a specifier is:
[[fill]align][sign][symbol][0][width][,][.precision][type]
The fill can be any character other than "{" or "}". The presence of a fill character is signaled by the character following it, which must be one of the align options.
The align can be:
The sign can be:
The symbol can be:
The "0" option enables zero-padding.
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 comma for a thousands separator.
The precision indicates how many digits should be displayed after the decimal point for a value formatted with types "f" and "%", or before and after the decimal point for a value formatted with types "g", "r" and "p".
The available type values are:
The type "n" is also supported as shorthand for ",g".
Returns the SI prefix for the specified value. If an optional precision is specified, the value is rounded accordingly using d3.round before computing the prefix. The returned prefix object has two properties:
For example:
var prefix = d3.formatPrefix(1.21e9); console.log(prefix.symbol); // "G" console.log(prefix.scale(1.21e9)); // 1.21
This method is used by d3.format for the s
format.
Returns the value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. The result is a number. Values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, the value is rounded up in accordance with the built-in round function. For example:
d3.round(1.23); // 1 d3.round(1.23, 1); // 1.2 d3.round(1.25, 1); // 1.3 d3.round(12.5, 0); // 13 d3.round(12, -1); // 10
Note that the resulting number when converted to a string may be imprecise due to IEEE floating point precision; to format a number to a string with a fixed number of decimal points, use d3.format instead.
Returns a quoted (escaped) version of the specified string such that the string may be embedded in a regular expression as a string literal.
d3.requote("[]"); // "\[\]"
See the d3.time module.
© 2010–2016 Michael Bostock
Licensed under the BSD License.
https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md