user3833682
user3833682

Reputation: 263

trailing zeros calculation in jquery with points values

Hi I'm trying to calculate values with standard way like 12,000.00 or 1,678,900.00 or 0.36 values but its not calculating my values if comma or point exists. here is my code jsFiddle

for example

I'm giving the values

Payment Sent 1: 12,000.00

Payment Sent 2: 1,678,900.00

Payment Sent 3: 0.36

Now Total Payment Sent convert into 0.00 values

Total Payment Sent: 0.00

So how to calculate these values properly.

Upvotes: 1

Views: 52

Answers (1)

Joy Biswas
Joy Biswas

Reputation: 6527

    function paymentSentSum() {
      var totalPaymentSent = 0;
      var that;
      $(".paymentSentSum").each(function() {
         that = parseFloat(this.value.replace(/\,/g,''));
         if(!isNaN(that) && that != 0) {
            totalPaymentSent += that;
         }
      });
      $("#paymentSentTotalPaymentSent").val((totalPaymentSent).formatMoney(2, '.', ','));
    }

    function addTrailingZerosBookings() {
      var that;
      $(".paymentSentSum").each(function() {
        that = parseFloat(this.value.replace(/\,/g,''));
        if(that && !isNaN(that)) {
            //$("#" + $(this).attr('id')).val(trailingZeros(this.value));
            if(isAlphaNumeric(that)) {
                $("#" + $(this).attr('id')).val(that.formatMoney(2, '.', ','));
            } else {
                $("#" + $(this).attr('id')).val(this.value);
            }
         }
      });
    }

Here use this. I have used parseFloat initially when to get the amount and then applied your checks. Rest all is good.

Code Pen : http://codepen.io/anon/pen/JdqLgB

Upvotes: 3

Related Questions