Tom
Tom

Reputation: 6697

Parse currency from table with jquery

How to safely parse the currency from table and compare if sum is higher than 19,90 ?

                    <tr class="Row1">
                      <td colspan="2" class="Column1 GrandTotal">Sum</td>
                      <td class="Money"><b>23,15 €</b></td>
                    </tr>

Upvotes: 0

Views: 1144

Answers (2)

tvanfosson
tvanfosson

Reputation: 532595

Assuming that you know that you always have two decimal digits and the same locale every time the following should work If not, then you'll need to check first and do the division and separator extraction optionally depending on the locale. Note that it's important to get the text so that you omit any enclosing HTML and get only the text nodes.

var total = 0;
$('table td.Money').each( function() {
     var amount = $(this).text().replace(/[,.]/g,'');
     total += parseFloat( amount ) / 100.0;
});
alert( total );

Upvotes: 1

Tim
Tim

Reputation: 5943

If you use dots instead of commas, you can easily get the value with parseFloat. http://jsfiddle.net/vKe7N/2/

If you want to keep the commas, you need so replace them with dots.

Upvotes: 0

Related Questions