www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    What javascript math functions should I be using?

    Hello, i need help with a script. I need decimals to the tenth place. my grandTotal adds a zero and my other totals show no decimals. Should I use parseInt(), parseFloat(), or Number()? What am I doing wrong?


    <script type="text/javascript">
    var LHtotal;
    var AHtotal;
    var LMtotal;
    var AMtotal;
    var schoolTotal;
    var churchTotal;
    var grandTotal;
    function calculateSchool(orgType) {
    var fund = Number(document.getElementById(orgType + 'fund').value);
    var people = Number(document.getElementById(orgType + 'people').value);
    var percent = (Number(document.getElementById(orgType + 'percent').value))/100;
    var active = people * percent;
    active = Number(active);
    var baskets = Number(document.getElementById(orgType + 'baskets').value);
    var numPerYear = Number(document.getElementById(orgType + 'numPerYear').value);
    var price = 26.00;
    var commision = 0.10;
    var total = fund * active * baskets * numPerYear * price * commision;
    total = total.toFixed();
    LHtotal = Number(document.getElementById("LHtotal").innerHTML);
    AHtotal = Number(document.getElementById("AHtotal").innerHTML);
    LMtotal = Number(document.getElementById("LMtotal").innerHTML);
    AMtotal = Number(document.getElementById("AMtotal").innerHTML);
    //alert(LHtotal + " " + AHtotal + " " + LMtotal + " " + AMtotal);
    schoolTotal = (LHtotal + AHtotal + LMtotal + AMtotal);
    //alert(schoolTotal);

    if(isNaN(schoolTotal)) {
    schoolTotal = "";
    }

    if(isNaN(active)) {
    active = "";
    }
    if(isNaN(total)) {
    total = "";
    }
    var Ototal = Number(document.getElementById("Ototal").innerHTML);
    var Etotal = Number(document.getElementById("Etotal").innerHTML);
    var orgTotal = (Ototal + Etotal);
    var orgTotal = orgTotal.toFixed();
    grandTotal = (schoolTotal + orgTotal);
    if(isNaN(grandTotal)) {
    grandTotal = "";
    }
    if(isNaN(orgTotal)) {
    orgTotal = "";
    }

    document.getElementById("orgTotal").innerHTML = orgTotal;
    document.getElementById("grandTotal").innerHTML = grandTotal;
    document.getElementById(orgType + 'total').innerHTML = total;
    document.getElementById(orgType + 'active').innerHTML = active;
    document.getElementById("schoolTotal").innerHTML = schoolTotal;


    }
    function calculateChurch(orgType) {
    var num = Number(document.getElementById(orgType + "num").value);
    var members = Number(document.getElementById(orgType + "members").value);
    var percent = Numbert(document.getElementById(orgType + "percent").value)/100;
    var baskets = Number(document.getElementById(orgType + "baskets").value);
    var fundPerYear = Number(document.getElementById(orgType + "fundPerYear").value);
    var total = num * members * percent * baskets * fundPerYear;
    total = total.toFixed();
    if(isNaN(total)) {
    total = "";
    }
    document.getElementById(orgType + 'total').innerHTML = total;
    var Ltotal = Number(document.getElementById('Ltotal').innerHTML);
    var Atotal = Number(document.getElementById('Atotal').innerHTML);
    churchTotal = (Ltotal + Atotal);
    churchTotal = churchTotal.toFixed();
    if(isNaN(churchTotal)) {
    churchTotal = "";
    }
    grandTotal += churchTotal;
    document.getElementById("churchTotal").innerHTML = churchTotal;
    document.getElementById("grandTotal").innerHTML = grandTotal;
    }
    function format(num, dec) {
    return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    }

    </script>

  2. #2
    Join Date
    Feb 2006
    Posts
    2,925
    When you use number.toFixed() with no argument you convert number to an integer with no decimal.
    number.toFixed(10) may be what you need.

    If you want to include trailing zeroes for numbers with less than 10 dignificant decimal digits you can
    use the string you get with with toFixed(10) and pad it with '0' characters.
    Last edited by mrhoo; 09-11-2012 at 02:17 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles