Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Created August 10, 2019 08:20
Show Gist options
  • Save jamesgeorge007/1b358cd5448ba1620d6d95955793b812 to your computer and use it in GitHub Desktop.
Save jamesgeorge007/1b358cd5448ba1620d6d95955793b812 to your computer and use it in GitHub Desktop.
Format numbers (USD)
const [, , input] = process.argv;
const arr = input.split('.');
const sec = arr.length > 1 ? '.' + arr[1] : '';
const subArr = arr[0].split('');
let res = subArr;
if (subArr.length === 4) {
res.splice(1, 0, ',');
} else {
res.splice(3, 0, ',');
for (let i = 4; i < subArr.length; i++) {
if (i % 4 === 2) {
res.splice(i + 1, 0, ',');
}
}
const index = res.slice(-1)[0];
if (index === ',') {
res.pop();
}
}
console.log(res.join('') + sec);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment