Skip to content

Instantly share code, notes, and snippets.

@droid001
Last active April 5, 2020 20:28
Show Gist options
  • Save droid001/5fc180335fafc75403059114041318e4 to your computer and use it in GitHub Desktop.
Save droid001/5fc180335fafc75403059114041318e4 to your computer and use it in GitHub Desktop.
Format the number to a string space padded every 3 decimals
/**
* Format the number to a string space padded every 3 decimals
*/
export const formatNumber = (n, suffix="")=>{
let num = String(n).split(".")[0];
let decimal = String(n).split(".")[1];
decimal = decimal ? "," + decimal : "";
let numStr = "";
let l = num.length-1;
let i = num.length;
let pos = 0;
for( i; i--;){
pos = l - i;
numStr = pos%3 === 0 ? num.charAt(i) + " " + numStr : num.charAt(i) + numStr;
}
return `${numStr.trim()}${decimal}${suffix}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment