1

I'm using this snippet to calculate the sum of two table cells.

var sum = 0;
$('.amount').each(function(){
    sum += parseFloat($(this).text());
});

$('.sum').text(sum);

But when the sum is for example 40.00 it displays as 40 and as picky as I am, i want it to display as 40.00. So... what to do?

Little (editable) demo: https://codepen.io/gentlemedia/pen/gGXJgR

Edited by gentlemedia

1
Contributor
1
Reply
37
Views
9 Months
Discussion Span
Last Post by gentlemedia
1

I've found the answer myself :)

There is toFixed() method, so I had to add this to the sumvariable with the 2 as for the 2 decimals;

$('.sum').text(sum.toFixed(2));

Edited by gentlemedia

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.