Created
August 10, 2019 08:20
-
-
Save jamesgeorge007/1b358cd5448ba1620d6d95955793b812 to your computer and use it in GitHub Desktop.
Format numbers (USD)
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
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