1

Possible Duplicate:
Display two decimal places, no rounding

I need to format the total result numbers in 2 decimal format. I am trying to achieve it without using jQuery plugin but just editing my following function:

function tally(selector) {
    var total = 0;
    $('p.editable_number').each(function() {
        total += parseInt($(this).text()) || 0;
        $('#subtotal').html(total)
        $('#total').html(total*0.21);
        $('#total1').html(total*1.21);
    })
}

How this is possible modifying the VAR? There are other ways to achieve it?

Here my case, as you can notice i dont get the total result formatted just with decimal separator

Community
  • 1
  • 1
Koala7
  • 1,340
  • 7
  • 41
  • 83

2 Answers2

1

If I understand correctly, you want to format your total so that it displays with 2 decimals.

Try this:

$('#subtotal').html(total.toFixed(2));

and so on...

Hope this helps.

Jivings
  • 22,834
  • 6
  • 60
  • 101
Luc Morin
  • 5,302
  • 20
  • 39
  • i am doing like this , it does not work neither $('#total1').html(total.toFixed(2)*1.21); $('#total').html(total.toFixed(2)*0.21) – Koala7 Oct 19 '12 at 11:34
  • 1
    You must do like this: $('total').html((total *0.21).toFixed(2)) – Luc Morin Oct 19 '12 at 11:41
1
$('#subtotal').html((total).toFixed(2))
$('#total').html((total*0.21).toFixed(2));
$('#total1').html((total*1.21).toFixed(2));

One note though: in generic case, where the base is not an integer, it's possible that base + vat != total because of rounding.

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57