1

Currently, I have the function:

function TotalAmount(field, container) {
    var total = 0;
    field.each(function () {
        total += $(this).val() * 1;
    });
    container.val(total.toFixed(2));
}

Example(1):
55.00 + 50.00 = 105.00 (this is correct)

Example (2):
This currently returns:
55 + 50 = 105.00

I need it to return 105 instead.

How do I change my functionality to accomplish this?

Andy G
  • 18,826
  • 5
  • 45
  • 66
Chaka
  • 1,657
  • 10
  • 33
  • 57
  • possible duplicate of [How do I convert a float to an int in Javascript?](http://stackoverflow.com/questions/596467/how-do-i-convert-a-float-to-an-int-in-javascript) – Nick Andriopoulos Sep 09 '13 at 14:13

3 Answers3

1

Try

function TotalAmount(field, container) {
    var total = 0;
    field.each(function () {
        total += $(this).val() * 1;
    });
    container.val(total == parseInt(total) ? total : total.toFixed(2));
}

Demo: Fiddle

Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
0

Try

 container.val(total == parseInt(total) ? total : total.toFixed(2));

parseInt

Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
0

Using modulous operator can solve ur issue:

If (total % 1 != 0)
return total
else 
return total / 100
Saeedses
  • 109
  • 1
  • 8