1 Reply Latest reply on Sep 17, 2014 8:20 AM by gkaiseril

    javascript calculator gives wrong answer when result is over $1000

    PreservedKillick

      I've been asked to fix a little script that calculates totals based on inputs to form fields.

       

      http://www.fongemie.com/wed.php

       

      It works fin until you go over $1000. Add 19 guests and it works, add 40 guests and it gives an incorrect total.

       

      I can't see where the issue is:

       

      <!--

              function formatCurrency(num) {

        num = num.toString().replace(/\$|\,/g,'');

        if(isNaN(num))

        num = "0";

        sign = (num == (num = Math.abs(num)));

        num = Math.floor(num*100+0.50000000001);

        cents = num%100;

        num = Math.floor(num/100).toString();

        if(cents<10)

        cents = "0" + cents;

        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)

        num = num.substring(0,num.length-(4*i+3))+','+

        num.substring(num.length-(4*i+3));

        return (((sign)?'':'-') + num + '.' + cents);

        }

             

              function calcprice(item_name, price1, price2, price3, price4, price5) {

              theform = document.forms["form1"]

              qty = eval("theform." + item_name + "_Quantity.value")

              price_field = eval("theform." + item_name + "_Price")

              total_field = eval("theform." + item_name + "_Total")

              if(!qty) qty = 0

              price = 0

              if(qty >=0 && qty <=1) price = price1

              if(qty >=2 && qty <=74) price = price2

              if(qty >=75 && qty <=99) price = price3

        if(qty >=100 && qty <=125) price = price4

              if(qty >=126 && qty <=150) price = price5

              if(qty >=0 && price <= 0) price = price1

            

              price_field.value = formatCurrency(price)

              total_field.value = formatCurrency(price * qty)

              //alert (qty)

            

              CalculateTotal()

            

              }

            

              function tonum(string1) {

              if (string1.substring(0,1) == '$') string1 = string1.substring(1,100)

              newval = parseFloat(string1)

              if(isNaN(newval)) return 0

              return newval

              }

            

              function CalculateTotal() {

              theform = document.forms["form1"]

            

              Order_Total = theform.Order_Total

              Order_Total.value = formatCurrency(

              tonum(theform.Afternoon_Total.value) +

        tonum(theform.Tent_Total.value) +

        tonum(theform.Tent_Pizza_Total.value) +

        tonum(theform.Evening_Total.value) +

        tonum(theform.Chairs_Total.value) +

              tonum(theform.Tent.value) +

              tonum(theform.Dance.value) +

        tonum(theform.Room.value) +

              tonum(theform.Coordinator.value)

              )

            

              //alert(theform.Canada_Ship.value.substring(0,1))

              //alert(parseFloat(theform.Gift_Pack1_Total.value))

              }

            

              -->

                </SCRIPT>