Thursday, 16 February 2017

JavaScript: Displaying number in indian format

Here is the solution for displaying number in comma seprated format in your page using javascript.

function FormatIndian(number) {
    try {
        var res = 0.00;
        var isNegative = false;
        var num = 0;
        //if (number != null || number != 'null' || number != undefined) {
        if (!num) {
            if (number < 0)
                isNegative = true;

            num = Math.abs(number);
            num = num.toFixed(2).toString();
            var lastThree = num.substring(num.length - 6);
            var otherNumbers = num.substring(0, num.length - 6);
            if (otherNumbers != '')
                lastThree = ',' + lastThree;
            res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;

            if (isNegative)
                res = "-" + res;
        }
        return res;
    } catch (e) {

    }
}
function toFixed(number, precision) {
    var multiplier = Math.pow(10, precision + 1),
        wholeNumber = Math.floor(number * multiplier);
    return Math.round(wholeNumber / 10) * 10 / multiplier;
}

No comments:

Post a Comment