javascript - Comma-formated numbers in jQuery - Stack Overflow

I have following function in jQuery to format the number into ma-formatted:function CommaFormattedN(amo

I have following function in jQuery to format the number into ma-formatted:

function CommaFormattedN(amount) {

    var delimiter = ","; 
    var i = parseInt(amount);

    if(isNaN(i)) { return ''; }

    i = Math.abs(i);

    var minus = '';
    if (i < 0) { minus = '-'; }

    var n = new String(i);
    var a = [];

    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }

    if (n.length > 0) { a.unshift(n); }

    n = a.join(delimiter);

    amount = minus + n;

    return amount;

}

I am calling this function like this on

 $('.text_field1').bind("focus blur change keyup", function(){    
 var $el = $(this);
    $el.val(CommaFormattedN($el.val()));
});

It's working fine, but the problem is, when number of digits increase from 5, it makes nothing. All digits are deleted and it starts again.

I have following function in jQuery to format the number into ma-formatted:

function CommaFormattedN(amount) {

    var delimiter = ","; 
    var i = parseInt(amount);

    if(isNaN(i)) { return ''; }

    i = Math.abs(i);

    var minus = '';
    if (i < 0) { minus = '-'; }

    var n = new String(i);
    var a = [];

    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }

    if (n.length > 0) { a.unshift(n); }

    n = a.join(delimiter);

    amount = minus + n;

    return amount;

}

I am calling this function like this on

 $('.text_field1').bind("focus blur change keyup", function(){    
 var $el = $(this);
    $el.val(CommaFormattedN($el.val()));
});

It's working fine, but the problem is, when number of digits increase from 5, it makes nothing. All digits are deleted and it starts again.

Share Improve this question edited Jul 10, 2015 at 12:02 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Feb 9, 2011 at 6:54 airair 6,26426 gold badges95 silver badges126 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can use Number.toLocaleString() function to format a number into a locale specific format. Note that the output of the function varies with regional settings:

var n = parseInt("-123456789", 10);
console.log(n.toLocaleString())
// returns -123,456,789 on my puter (english-us locale)
// returns -123 456 789 for french locale
// returns -123.456.789 for german locale
// returns -123'456'789 for romansh (???) locale
  1. There is a bug related to sign. You are using absolute value instead of original one.
  2. Your code seems to work fine, you can try debugging the way you are calling your function.

Here is your working code with minor edit ( sign related ) : http://jsfiddle/qcVDc/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745344216a4623468.html

相关推荐

  • javascript - Comma-formated numbers in jQuery - Stack Overflow

    I have following function in jQuery to format the number into ma-formatted:function CommaFormattedN(amo

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信