Skip to content

Instantly share code, notes, and snippets.

@worc
worc / format-number-with-si-prefix.js
Last active September 13, 2022 06:33 — forked from cho45/format-number-with-si-prefix.js
but with mu for micro
function formatN (n) {
const unitList = ['y', 'z', 'a', 'f', 'p', 'n', 'μ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
const zeroIndex = 8;
const nn = n.toExponential(2).split(/e/);
let u = Math.floor(+nn[1] / 3) + zeroIndex;
if (u > unitList.length - 1) {
u = unitList.length - 1;
} else
if (u < 0) {
u = 0;