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 = "";
}
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.
Bookmarks