Sunday, January 20, 2013

[JQuery] Add a character to a specific position

It's not uncommon to want to add a separator dot or comma to improve visuals on a big number. Here is how.
function getValueWithThousandDot(value) {
        var newValue = value.toString();
        b = ".";
        position = 3;
        if (newValue.length >= 4) {
            newValue = [newValue.slice(0, newValue.length - position), b, newValue.slice(newValue.length - position, newValue.length)].join('');
        }

        return newValue;
    }

No comments:

Post a Comment