Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3: Why is d3.format(".2s") formatting -0.5 as -500m?

I have a d3 scale. The tick values are fixed at [0, -0.5, -1]. With the tick format set to ".2s" the value -0.5 is displayed as -500m. Without the tick format set, the value is displayed correctly.

with fomat

without format

like image 584
William Moore Avatar asked Sep 17 '25 21:09

William Moore


2 Answers

Formatting function works as properly in your case. format(".2s") it means "SI-prefix with two significant digits" - from docs. There is awesome demo-page when you can choose formatting function for you - http://bl.ocks.org/zanarmstrong/05c1e95bf7aa16c4768e

like image 114
Mikhail Shabrikov Avatar answered Sep 19 '25 10:09

Mikhail Shabrikov


According to the documentation at https://github.com/d3/d3-format the format you've chosen is defined as:

d3.format(".2s")(42e6); // SI-prefix with two significant digits, "42M"

Wikipedia states the preference of International System of Units:

Prefixes corresponding to an integer power of one thousand are generally preferred. Hence 100 m is preferred over 1 hm (hectometre) or 10 dam (decametres)...

Thus, .5 in the format you've chosen comes to 500m which is preferred display for the format.

like image 34
rgthree Avatar answered Sep 19 '25 12:09

rgthree