question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

d3.format() for number abbreviations (similar to SI-prefix)

See original GitHub issue

I’m wondering if anyone else would find a use for a feature like this. For example, SI-prefix will condense large (or small) numbers and add the appropriate metric system character:

var parse = d3.format("s");

console.log(parse(5000000000)); //5G
console.log(parse(5000000)); //5M
console.log(parse(.0005)); //500µ

I want to create a similar function, let’s just call it d3.format("a") that will condense numbers to human readable abbreviations, so long numbers are more easily inserted into charts and axes.

var parse = d3.format("a");

console.log(parse(5000000000)); //5B
console.log(parse(5000000)); //5M
console.log(parse(5000)); //500k

Possible prefixes could be ‘T’ for trillion, ‘B’ for billion, ‘M’ for million, ‘k’ for thousand, plus any others we may need. I don’t mind writing the functionality if @mbostock thinks it’s a good idea. I know the argument against it is to just convert the number yourself, but then the prefix isn’t being decided in real time, or you’d have to have a lot of separate if/else cases.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:10
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
mbostockcommented, Oct 22, 2015

Hmm. One way you could do this would be to remap the SI prefix after invoking the format. For example:

var formatSi = d3.format(".3s");

function formatAbbreviation(x) {
  var s = formatSi(x);
  switch (s[s.length - 1]) {
    case "G": return s.slice(0, -1) + "B";
  }
  return s;
}

formatAbbreviation(5000000000); // 5.00B
formatAbbreviation(5000000); // 5.00M
formatAbbreviation(5000); // 5.00k

(Note, that’s 5.00k, not 500k as in your example. I think 5.00k is correct?)

Another way might be to localize the list of SI prefixes, rather than having a prefixes global. (See src/locale.js in d3-format.) But, do you really want to specify the full list of 17 prefixes this way? Seems like you only care about 3 or 4… assuming you are dealing with dollar amounts, say, and not arbitrary numbers.

The other simple thing you can do is to just roll your own conditional format.

var formatNumber = d3.format(".0f"),
    formatBillion = function(x) { return formatNumber(x / 1e9) + "B"; },
    formatMillion = function(x) { return formatNumber(x / 1e6) + "M"; },
    formatThousand = function(x) { return formatNumber(x / 1e3) + "k"; };

function formatAbbreviation(x) {
  var v = Math.abs(x);
  return (v >= .9995e9 ? formatBillion
      : v >= .9995e6 ? formatMillion
      : formatThousand)(x);
}

formatAbbreviation(5000000000); // 5B
formatAbbreviation(5000000); // 5M
formatAbbreviation(5000); // 5k

I mean, yeah, your formatAbbreviation has to deal with rounding and is hard-coded to zero decimal places (in this case), but you can probably guess how to improve that if you need it… or look at how formatPrefix is implemented.

11reactions
pdlaruecommented, May 17, 2017

For those who are looking for a solution. We replaced .tickFormat(d3.dormat(“s”)); with the following function: .tickFormat (function (d) { return formatValue(d).replace(‘G’, ‘B’); }); Worked like a charm!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Format a number with SI Prefix, with fixed number of decimals
You almost got it right. Using d3.formatPrefix() one can get the SI prefix. To get the rounded number without decimals, I used Javascript's...
Read more >
d3.format
d3 -format helps you format numbers for human consumption. ... formatPrefix returns a formatter with a consistent SI prefix rather than choosing one...
Read more >
D3: Why is d3.format(".2s") formatting -0.5 as -500m?-d3.js
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 ......
Read more >
Formatting - D3 wiki
D3 makes this easy using a standard number format. ... SI-prefix ("s") - like rounded, but with a unit suffixed such as "9.5M"...
Read more >
How to Format Numbers, Dates, and Time Using D3 ...
%b - abbreviated month name; %B - full month name; %d - zero-padded day of the month as a decimal number [01,31] ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found