Last active
April 5, 2020 20:28
-
-
Save droid001/5fc180335fafc75403059114041318e4 to your computer and use it in GitHub Desktop.
Format the number to a string space padded every 3 decimals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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