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
  • 19,232
  • 5
  • 47
  • 69
Chaka
  • 1,709
  • 11
  • 33
  • 58
  • 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
  • 384,651
  • 66
  • 527
  • 531
0

Try

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

parseInt

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

Using modulous operator can solve ur issue:

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