I have a price number that looks like this: $27,272.70000
I’m trying to make it look like this: $27,272.70
I’m stuck with trying different methods but here is what i’ve got so far:
jQuery(document).ready(function() {
jQuery('.cart-table-wrapper #shopping-cart-table tbody > tr').each(function() {
var the_sp_subtotal = jQuery(this).find('td.col-total.a-right span.price').text().replace("$", "");
var new_sp_subtotal = parseFloat(the_sp_subtotal).toFixed(2);
console.log(new_sp_subtotal);
});
});
But the result that I get is: 27.00
Here is the fiddle – https://jsfiddle.net/zqe37xsk/1/
Can someone please help me, what I’m doing wrong?
Thank you
2
Answers
parseFloat("27,272.70")
returns27
because the,
in27,272.70
is no longer part of a number.As an alternative approach you could replace the part behind the last thousands separator and call
toFixed
on that. Then you can just join everything back together.In your
each
function, use:To format a currency string properly you could use the
toLocaleString
function. For this to properly work you have to transform your string into a float.