Accepted answer

The decision in this case does not depend on D3.

D3 is, obviously, a JavaScript library. Internally, d3.format uses Number.prototype.toPrecision(). We can see this in the source code:

export default {
    "g": function(x, p) { return x.toPrecision(p); },

So, for describing the behaviour (that is, how the choice is made), we have to look at the ECMAScript documentation for Number.prototype.toPrecision(). We can find it at § The most relevant part for this question is that one at subsection 12.c, pay attention to the -6 there: Number.prototype.toPrecision ( precision )

Return a String containing this Number value represented either in decimal exponential notation with one digit before the significand's decimal point and precision–1 digits after the significand's decimal point or in decimal fixed notation with precision significant digits. If precision is undefined, call ToString (7.1.12) instead. Specifically, perform the following steps:


12.c. If e < –6 or e ≥ p, then


vii. Return the concatenation of s, m, code unit 0x0065 (LATIN SMALL LETTER E), c, and d.

As we can see, numbers smaller than 0.000001 (that is, 1e-6) will be returned using exponential notation.

Let's check it:

<script src=""></script>

As we can see, 0.000001 will be logged as regular decimal notation and not as 1e-6.

Pay attention to the fact that if no precision is set the d3.format for that specifier defaults to 6 significant figures (hence 0.00000100000), and contrary of what most people thing those five zeros at the right of the 1 are significant figures.

If you want to show just the digits until the 1, let's set the precision:

<script src=""></script>

That being clear, let's see what happens if we use 0.0000001, which is 1e-7:

<script src=""></script>

As you can see it returns 1.00000e-7 instead of 0.0000001. And the same code, using 1 as precision:

<script src=""></script>


It seems to me that you cannot change that behaviour using D3 only. Your number (0.00000020852945934254138) is smaller than 1e-6, and therefore the exponential notation is expected here. For changing that you'll have to write your own customised function.

Related Query

More Query from same tag