2

i have used this jQuery code

jQuery.fn.digits = function(){ 
    return this.each(function(){ 
        jQuery(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

for number 150000 and i want it to out put like 150,000

but it is outputting this : 150,000.00

i don't want these extra .00

bhr
  • 33
  • 3
  • 1
    So what have you tried to resolve this? – putvande Jan 27 '16 at 22:11
  • Actually i don't understand the regex that why here to get some help. – bhr Jan 27 '16 at 22:12
  • Try this http://openexchangerates.github.io/accounting.js/ – Ashok Vishwakarma Jan 27 '16 at 22:14
  • So for what kind of numbers should this work? Just 150000 or any? – putvande Jan 27 '16 at 22:14
  • this will be any number 150000 is just for example. – bhr Jan 27 '16 at 22:15
  • It should be working. Are you sure that the $(this).text() returns "150000"? You can see it working there: https://regex101.com/r/wO8cB4/1 – anolsi Jan 27 '16 at 22:15
  • I would suggest looking the used signs up since this is a very easy regex. [This](http://www.regular-expressions.info/tutorial.html) page explains it well. – DIDoS Jan 27 '16 at 22:16
  • 2
    There is nothing in your code that would add `.00` to your number. Are you sure the original is `150000` and not `150000.00`? – Niet the Dark Absol Jan 27 '16 at 22:20
  • Also your regex is very simple: "A digit, which is followed by a multiple of 3 digits, with no further digits beyond." – Niet the Dark Absol Jan 27 '16 at 22:21
  • Your code works for me. `.00` is only in the result if it's also in the input. See https://jsfiddle.net/barmar/7srLktto/2/ – Barmar Jan 27 '16 at 23:47
  • Take a look at my [answer to this question](http://stackoverflow.com/questions/23966148/how-can-i-format-currency-by-using-jquery/23966200), you should be able to use this little function to handle what you need with a few tweaks. – faino Jan 27 '16 at 23:49

3 Answers3

2

I find a for loop is easier to understand than a regex.

function addCommas(num) {
    var characters = parseInt(num, 10).toString();
    var output = '';
    for (var offset = characters.length; offset > 0; offset -= 3) {
        output = characters.slice(Math.max(offset - 3, 0), offset) + (output ? ',' + output : '');
    }
    return output;
}
Nick White
  • 2,063
  • 16
  • 10
  • @charlietfl thanks, edited to include a radix. Also note that this will round the input number to an integer, losing any digits after the decimal point. It's an easy exercise to capture those and append them back at the end, if needed. – Nick White Jan 27 '16 at 22:21
  • The code in the question only outputs digits after the decimal if they're in the input. That seems to be what the OP is complaining about. – Barmar Jan 27 '16 at 23:48
1

You can use the JQuery Number Formatter plugin, so you do not have to deal with such regular expressions to format your number:

Jquery Number Formatter

Another work around would be to just trim the last 3 characters with

$(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,").slice(0,-3)
0

Try Math.round before your regex then substring after - this is untested

var newnumber = Math.round(parseInt($(this).text());
var withcommas = newnumber.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
$(this).text(withcommas.substring(0, s.indexOf('.')));
MistaJase
  • 839
  • 7
  • 12