tags:

views:

21

answers:

2

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>
A: 

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.

Tim
+1  A: 

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 );
tvanfosson
Why not simply s/,/./ and parseFloat without the division by 100?
Sorpigal
thanks, now its summing the column, how to get just grandtotal
Tom
@Sorpigal - what about 100.000,00?
tvanfosson
@tvanfosson - Obviously you should also strip periods first. One must presume to know which format the number is in, otherwise any transformation is meaningless. I suggest that you should s/.//g; then s/,/./g; and parseFloat. My approach fails if the number uses the opposite convention, yours fails if someone neglected to supply exactly two post-decimal digits. One must choose the tradeoff that makes the most sense.
Sorpigal