Skip to content

Instantly share code, notes, and snippets.

@webinista
Last active June 14, 2018 01:18
Show Gist options
  • Save webinista/11325274 to your computer and use it in GitHub Desktop.
Save webinista/11325274 to your computer and use it in GitHub Desktop.
Format a number with a separator.
const numberFormat = (number, separator = ',') => {
const num = Number.isNaN(+number) ? 0 : +number;
let x = 0;
let fnum = [];
const digits = (num + '').split('');
const seg = digits.length / 3;
while(x < seg){
fnum[x] = digits.splice(-3).join('');
x++;
}
return fnum.reverse().join(separator);
}
numberFormat(100,','); // 100
numberFormat(1000,' '); // 1 000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment