0

I've make the calculation but the main problem right now is the answer after calculation for decimal point are incorrect.

 <script type="text/javascript">  
    
    function CalculateTotals() {  
        var gv = document.getElementById("<%= GridView1.ClientID %>");  
        var tb = gv.getElementsByTagName("input");  
        var lb = gv.getElementsByTagName("span");  

        var sub = 0;  
        var total = 0;  
        var indexQ = 1;  
        var indexP = 0;  
        var price = 0;  

        for (var i = 0; i < tb.length; i++) {  
            if (tb[i].type == "text") {  
                ValidateNumber(tb[i]);  

                price = lb[indexP].innerHTML.replace
                ("$", "").replace(",", "");  
                sub = parseFloat(price) * parseFloat(tb[i].value);  
                if (isNaN(sub)) {  
                    lb[i + indexQ].innerHTML = "0.00";  
                    sub = 0;  
                }  
                else {  
                    lb[i + indexQ].innerHTML = 
                    FormatToMoney(sub, "$", ",", "."); ;  
                }  
                 
                indexQ++;  
                indexP = indexP + 2;  

                total += parseFloat(sub);  
            }  
        }  

        lb[lb.length - 1].innerHTML = 
        FormatToMoney(total, "$", ",", ".");  
    }  

    function ValidateNumber(o) {  
        if (o.value.length > 0) {  
            o.value = o.value.replace(/[^\d]+/g, ''); //Allow only whole numbers  
        }  
    } 

    function isThousands(position) {  
        if (Math.floor(position / 3) * 3 == position) return true;  
        return false;  
    };  

    function FormatToMoney(theNumber, theCurrency, theThousands, theDecimal) {  
        var theDecimalDigits = Math.round((theNumber * 100) - (Math.floor(theNumber) * 100));  
        theDecimalDigits = "" + (theDecimalDigits + "0").substring(0, 2);  
        theNumber = "" + Math.floor(theNumber);  
        var theOutput = theCurrency;  
        for (x = 0; x < theNumber.length; x++) {  
            theOutput += theNumber.substring(x, x + 1);  
            if (isThousands(theNumber.length - x - 1) && (theNumber.length - x - 1 != 0)) {  
                theOutput += theThousands;  
            };  
        };  
        theOutput += theDecimal + theDecimalDigits;  
        return theOutput;  
    }   
 </script>

When I'm add quantity value 2 * with price 16.52, the answer supposedly to be 33.04 but it show 33.40. Why am I getting inaccuracies in the answer.

kimk
  • 83
  • 6
  • 1
    replace `theDecimalDigits = Math.round((theNumber * 100) - (Math.floor(theNumber) * 100)); theDecimalDigits = "" + (theDecimalDigits + "0").substring(0, 2);` with `theDecimalDigits = theNumber.toFixed(2).split('.')[1];` – NIKUNJ PATEL Feb 21 '23 at 05:50
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Feb 21 '23 at 05:52

1 Answers1

0

You need to replace your convert price lines of code with toFixed function.

Replace

theDecimalDigits = Math.round((theNumber * 100) - (Math.floor(theNumber) * 100));   
theDecimalDigits = "" + (theDecimalDigits + "0").substring(0, 2);

With

theDecimalDigits = theNumber.toFixed(2).split('.')[1];
NIKUNJ PATEL
  • 2,034
  • 1
  • 7
  • 22