I promise that I've done my homework on this. It's a basic question, but I'm not terribly familiar with Jquery. Assume the following...
var cac = parseInt($("#currentAccess").val());
var acn = parseInt($("#addCapital").val());
var tn = (cac + acn);
How do I then take the variable tn, do a replace on it, and output it? This is an example that doesn't work...
$("#totalNeedG").append("<p>$" + tn.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") + "</p>");
Thanks for this simple help!
I promise that I've done my homework on this. It's a basic question, but I'm not terribly familiar with Jquery. Assume the following...
var cac = parseInt($("#currentAccess").val());
var acn = parseInt($("#addCapital").val());
var tn = (cac + acn);
How do I then take the variable tn, do a replace on it, and output it? This is an example that doesn't work...
$("#totalNeedG").append("<p>$" + tn.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") + "</p>");
Thanks for this simple help!
Share Improve this question edited Jan 28, 2011 at 23:12 ThiefMaster 319k85 gold badges607 silver badges646 bronze badges asked Jan 28, 2011 at 23:08 Vaughn D. TaylorVaughn D. Taylor 612 silver badges3 bronze badges 2- re-tagged it to javascript as the question itself is not related to jquery – ThiefMaster Commented Jan 28, 2011 at 23:13
- possible duplicate of stackoverflow./questions/3883342/… – Raynos Commented Jan 28, 2011 at 23:13
2 Answers
Reset to default 5.replace()
is a String method. Not a Number method.
Convert it to a String.
var tn = ((cac + acn) + '');
String(yourNumber)
returns a string containing your number.
To convert it back, use parseInt(yourString, 10)
.
And, as you are not doing it, ALWAYS use the second argument if parseInt(str, base)
! If you don't, the base is determined automatically which is almost never what you want:
parseInt('010') == 8 // 0-prefix means octal
parseInt('0xff') == 255 // 0x-prefix means hex
While it's unlikely that somebody puts a number starting with 0x
in a field chances are high someone puts a leading zero for some reason.