Last active
September 8, 2017 06:14
-
-
Save ChrisVilches/52c76e0dc07ae38439a9eed90b524008 to your computer and use it in GitHub Desktop.
Javascript number formatting 12345 → 12.345
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
function dots(number, separator = '.') { | |
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, separator); | |
} | |
// Positive tests | |
console.assert(dots(0) === "0"); | |
console.assert(dots(1) === "1"); | |
console.assert(dots(12) === "12"); | |
console.assert(dots(123) === "123"); | |
console.assert(dots(1234) === "1.234"); | |
console.assert(dots(12345) === "12.345"); | |
console.assert(dots(123456) === "123.456"); | |
console.assert(dots(1234567) === "1.234.567"); | |
console.assert(dots(12345678) === "12.345.678"); | |
console.assert(dots(123456789) === "123.456.789"); | |
console.assert(dots(1234567891) === "1.234.567.891"); | |
console.assert(dots(10) === "10"); | |
console.assert(dots(100) === "100"); | |
console.assert(dots(1000) === "1.000"); | |
console.assert(dots(10000) === "10.000"); | |
console.assert(dots(100000) === "100.000"); | |
console.assert(dots(1000000) === "1.000.000"); | |
console.assert(dots(10000000) === "10.000.000"); | |
console.assert(dots(100000000) === "100.000.000"); | |
console.assert(dots(1000000000) === "1.000.000.000"); | |
console.assert(dots(1001050003) === "1.001.050.003"); | |
// Negative tests | |
console.assert(dots(-0) === "0"); | |
console.assert(dots(-1) === "-1"); | |
console.assert(dots(-12) === "-12"); | |
console.assert(dots(-123) === "-123"); | |
console.assert(dots(-1234) === "-1.234"); | |
console.assert(dots(-12345) === "-12.345"); | |
console.assert(dots(-123456) === "-123.456"); | |
console.assert(dots(-1234567) === "-1.234.567"); | |
console.assert(dots(-12345678) === "-12.345.678"); | |
console.assert(dots(-123456789) === "-123.456.789"); | |
console.assert(dots(-1234567891) === "-1.234.567.891"); | |
console.assert(dots(-10) === "-10"); | |
console.assert(dots(-100) === "-100"); | |
console.assert(dots(-1000) === "-1.000"); | |
console.assert(dots(-10000) === "-10.000"); | |
console.assert(dots(-100000) === "-100.000"); | |
console.assert(dots(-1000000) === "-1.000.000"); | |
console.assert(dots(-10000000) === "-10.000.000"); | |
console.assert(dots(-100000000) === "-100.000.000"); | |
console.assert(dots(-1000000000) === "-1.000.000.000"); | |
// Using custom separator | |
console.assert(dots(0, ',') === "0"); | |
console.assert(dots(1, ',') === "1"); | |
console.assert(dots(12, ',') === "12"); | |
console.assert(dots(123, ',') === "123"); | |
console.assert(dots(1234, ',') === "1,234"); | |
console.assert(dots(12345, ',') === "12,345"); | |
console.assert(dots(-123456, ',') === "-123,456"); | |
console.assert(dots(-1234567, ',') === "-1,234,567"); | |
console.assert(dots(-12345678, '\'') === "-12'345'678"); | |
console.assert(dots(-123456789, '\'') === "-123'456'789"); | |
console.assert(dots(-1234567891, '\'') === "-1'234'567'891"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment