js number format space 1

js number format space

function numberWithSpaces(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " ");
    return parts.join(".");

Here is what the above code is Doing:
1. We’re creating a function called numberWithSpaces.
2. We’re passing in a variable called x.
3. We’re splitting the number into two parts, the whole number and the decimal.
4. We’re replacing the whole number with a space every three digits.
5. We’re joining the whole number and the decimal back together.
6. We’re returning the number.

Similar Posts