0
How can I treat this string to return only 2 decimals after the point?
$(".subtotal2").text(23.90);
var sum = 0;
$(".subtotal2").each(function() {
var val = $.trim( $(this).text() );
if (val) { val = parseFloat( val.replace( /^\$/, "" ) ); sum += !isNaN( val ) ? val : 0; }
});
$(".todo_total").text(sum*12);
This code for now returns me this:
Do not use
float
to represent money in Javascript, if possible. Use integer values representing the smallest possible unit (cents).– mgibsonbr
Related: http://answall.com/questions/11018/como-representa-dinheiro-em-javascript & also: http://answall.com/questions/14728/performs%C3%A7%C3%A3o-de-floating-point-beads-javascript-with-precis%C3%A3o-absolute
– Sergio