Skip to content

Instantly share code, notes, and snippets.

@pseudosavant
Last active January 26, 2022 16:58
Show Gist options
  • Save pseudosavant/ca1863df1e1fda4899c7ed453c247609 to your computer and use it in GitHub Desktop.
Save pseudosavant/ca1863df1e1fda4899c7ed453c247609 to your computer and use it in GitHub Desktop.
Divide any number/string every nth characters
const divide = (str, divider, n) => String(str)
.split('')
.reverse()
.reduce((acc, cv, i) => cv + (i % n === 0 ? divider : '') + acc);
let num = 4000000;
divide(num, '|', 2); // 4|00|00|00
divide(num, ',', 3); // 4,000,000
divide(num, '-', 1); // 4-0-0-0-0-0-0
let cc = 1000200030004000;
divide(cc, '-', 4); // 1000-2000-3000-4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment