You understand almost everything. Using d3.formatPrefix() , you can get the SI prefix. To get a rounded number without decimals, I used Javascript .toFixed() :
var prefix = d3.formatPrefix(137594020); console.log(prefix.symbol); // "M" console.log(prefix.scale(137594020).toFixed()); // 138 var prefix = d3.formatPrefix(13759402); console.log(prefix.symbol); // "M" console.log(prefix.scale(13759402).toFixed()); // 14 var prefix = d3.formatPrefix(1375); console.log(prefix.symbol); // "k" console.log(prefix.scale(1375).toFixed()); // 1
You can try it yourself: jsfiddle .
Bengt
source share