We help IT Professionals succeed at work.

How can I remove dollar sign ($) from a string when parsing it?

I  have the attached function that adds values in a table column and dispalys the value in a span.  Got it.  I've since changed the numbers coming in to the table so they have $ currency notation.  How can I strip that away before performing the math?  I've tried adding .replace("$","") in several places, but it isn't working -- either because it just doesn't work or I'm doing something wrong.

Ideas?

Jeremy
var totalOwed = 0;
var rowsOwed = $("#summaryTable tr:gt(0)");
rowsOwed.children("td:nth-child(3)").children("span").each(function() {
    totalOwed += parseFloat($(this).html());
});
$("#ctl00_ContentPlaceHolder1_lblTotalOwedProject").html("$" + totalOwed.toFixed(2));

Open in new window

Comment
Watch Question

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi mrcoulson,,
Try : http://www.w3schools.com/jsref/jsref_replace.asp 
Normal string.replace. It will get you the results you want.
Regards,
Chinmay


Author

Commented:
That's what I've been trying, but it's not working.  Any clues as to where I should be using it?

Jeremy
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
I think you only will be able to tell that. However only two operations I see doubtful
1. totalOwed += parseFloat($(this).html());  - This one does the total
2. $("#ctl00_ContentPlaceHolder1_lblTotalOwedProject").html("$" + totalOwed.toFixed(2)); - This one displays it.
Now where you need it?
if it is during that addition operation where your total is being counted than
var amountValue = $(this).html();
totalOwed += parseFloat(amountValue.toString().replace("$",""));
Let me know if it helps or not.
Regards,
Chinmay


Author

Commented:
That gets me a number, but I know it's wrong.  Check out the screen shot.  The sum of the numbers in blue is not $313.

Let me try it on a less complex example.  The values in this table are being supplied by database queries in my codebehind.  I know the calculations were correct, however, because I added $ to them all.

Jeremy
total.gif
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Alright let's say
rather than
totalOwed += parseFloat(amountValue.toString().replace("$",""));
try
var amountValue = amountValue.toString().replace("$","").replace(",",""));;
totalOwed += parseFloat(amountValue);
Regards,
Chinmay


Multitechnician
Distinguished Expert 2019
Commented:
Use :  totalOwed += parseFloat($(this).html().replace(/[$,]/g,""));

test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		var totalOwed = 0;
		var rowsOwed = $("#summaryTable tr:gt(0)");
		rowsOwed.children("td:nth-child(3)").children("span").each(function() {
			totalOwed += parseFloat($(this).html().replace(/[$,]/g,""));
		});
		$("#ctl00_ContentPlaceHolder1_lblTotalOwedProject").html("$" + totalOwed.toFixed(2));
	});
</script>
</head>
<body>
Total :&nbsp;<label for="medable" id="ctl00_ContentPlaceHolder1_lblTotalOwedProject"></label>
<br />
<br />
<table id="summaryTable">
<tr>
<th>Beneficiary</th>
<th>Amount/Unit</th>
<th>Amount Ordered Owed</th>
<th>Amount Collected</th>
</tr>
<tr><td><span>A</span></td><td><span>$2,136.00</span></td><td><span>$27,768.00</span></td><td><span></span></td></tr>
<tr><td><span>F</span></td><td><span>$720.00</span></td><td><span>$9,360.00</span></td><td><span></span></td></tr>
<tr><td><span>S</span></td><td><span>$19,189.00</span></td><td><span>$249,457.00</span></td><td><span></span></td></tr>
<tr><td><span>L</span></td><td><span>$267.00</span></td><td><span>$3,471.00</span></td><td><span></span></td></tr>
<tr><td><span>G</span></td><td><span>$0.00</span></td><td><span>$0.00</span></td><td><span></span></td></tr>
<tr><td><span>T</span></td><td><span>$1,000.00</span></td><td><span>$13,000.00</span></td><td><span></span></td></tr>
<tr><td><span>H</span></td><td><span>$0.00</span></td><td><span>$0.00</span></td><td><span></span></td></tr>
<tr><td><span>S</span></td><td><span>$658.00</span></td><td><span>$8,554.00</span></td><td><span></span></td></tr>
<tr><td><span>A</span></td><td><span>$320.00</span></td><td><span>$4,160.00</span></td><td><span></span></td></tr>
</table>
</body>
</html>

Open in new window

leakim971Multitechnician
Distinguished Expert 2019
Commented:
With the thousands separators : http://www.merlyn.demon.co.uk/js-maths.htm#DTS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		var totalOwed = 0;
		var rowsOwed = $("#summaryTable tr:gt(0)");
		rowsOwed.children("td:nth-child(3)").children("span").each(function() {
			totalOwed += parseFloat($(this).html().replace(/[$,]/g,""));
		});
		$("#ctl00_ContentPlaceHolder1_lblTotalOwedProject").html("$" + FComma(totalOwed.toFixed(2)));
	});

	function FComma(SS){
		var T='', S=String(SS), L=S.length-1, C, j, P = S.indexOf('.')-1;
		if (P<0) P=L;
		for (j=0; j<=L; j++) {
			T+=C=S.charAt(j);
			if ((j < P) && ((P-j)%3 == 0) && (C != '-')) T+=',';
		}
		return T
	}
  
</script>
</head>
<body>
Total :&nbsp;<label for="medable" id="ctl00_ContentPlaceHolder1_lblTotalOwedProject"></label>
<br />
<br />
<table id="summaryTable">
<tr>
<th>Beneficiary</th>
<th>Amount/Unit</th>
<th>Amount Ordered Owed</th>
<th>Amount Collected</th>
</tr>
<tr><td><span>A</span></td><td><span>$2,136.00</span></td><td><span>$27,768.00</span></td><td><span></span></td></tr>
<tr><td><span>F</span></td><td><span>$720.00</span></td><td><span>$9,360.00</span></td><td><span></span></td></tr>
<tr><td><span>S</span></td><td><span>$19,189.00</span></td><td><span>$249,457.00</span></td><td><span></span></td></tr>
<tr><td><span>L</span></td><td><span>$267.00</span></td><td><span>$3,471.00</span></td><td><span></span></td></tr>
<tr><td><span>G</span></td><td><span>$0.00</span></td><td><span>$0.00</span></td><td><span></span></td></tr>
<tr><td><span>T</span></td><td><span>$1,000.00</span></td><td><span>$13,000.00</span></td><td><span></span></td></tr>
<tr><td><span>H</span></td><td><span>$0.00</span></td><td><span>$0.00</span></td><td><span></span></td></tr>
<tr><td><span>S</span></td><td><span>$658.00</span></td><td><span>$8,554.00</span></td><td><span></span></td></tr>
<tr><td><span>A</span></td><td><span>$320.00</span></td><td><span>$4,160.00</span></td><td><span></span></td></tr>
</table>
</body>
</html>

Open in new window

Author

Commented:
I'll try these out ASAP. I'm distracted by a sick 2-year-old right now.

Jeremy

Author

Commented:
leakim971 nailed it and even provided some background so I'm learning.  Thanks, dude(tte)!

Jeremy