Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Decimal point calculation

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.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading