View Full Version : Decimal points not showing up
Flakbait
09-12-2002, 05:00 PM
function App(form)
{
{
var soc = parseInt(form.soc.value);
var parb = parseInt(form.parb.value);
var ind = parseInt(form.ind.value);
var wage = parseInt(form.wage.value);
var invest = parseInt(form.invest.value);
var other = parseInt(form.other.value);
var nurse = parseInt(form.nurse.value);
var home = parseInt(form.home.value);
var pre = parseInt(form.pre.value);}
{var Total = soc + parb + ind + wage + invest + other;
var Deduct = nurse + home + pre;
var Newtotal = Total - Deduct;
form.total.value = Total;
form.deduct.value = Deduct;
form.newtotal.value = Newtotal;
}
}
For some reason, this script refuses to allow any decimal numbers in the equasion. It doesn't round up or down, it just ignores them. Is there a way to make this script allow decimal points?
Thanks in advance
-------------------
Flakbait
beetle
09-12-2002, 05:12 PM
parseInt()!!!
parseInt() parses a string and retrieves any INTEGER value. Change those to parseFloat() and you should be good. Oh, and if you can, answer me this...function App(form) {
{ <-- ???
var soc = parseFloat(form.soc.value);
var parb = parseFloat(form.parb.value);
var ind = parseFloat(form.ind.value);
var wage = parseFloat(form.wage.value);
var invest = parseFloat(form.invest.value);
var other = parseFloat(form.other.value);
var nurse = parseFloat(form.nurse.value);
var home = parseFloat(form.home.value);
var pre = parseFloat(form.pre.value);
} <-- ???
{ <-- ???
var Total = soc + parb + ind + wage + invest + other;
var Deduct = nurse + home + pre;
var Newtotal = Total - Deduct;
form.total.value = Total;
form.deduct.value = Deduct;
form.newtotal.value = Newtotal;
} <-- ???
}What are all those extra curly brackets supposed to be doing? :D
Flakbait
09-12-2002, 05:36 PM
I changed the parseInt to parseFloat, even tried Number, and now it's spitting out non-existant numbers. It came up with "17575.890000000003" and "16497.440000000002" for the Total and Newtotal values, yet Deduct was off by merely 0.01. I've heard of this problem before but the author ended up with a mile-long script to get around it. This result was from running through the script once, and not one number set had more than a two-place (e.g. 40.44) decimal value! Sure has me scratchin my head :confused:
Thanks again!
--------------------
Flakbait
beetle
09-12-2002, 05:55 PM
Hrmmm...perhaps this will do the trickfunction roundedDecimal(decimal,zeros) {
var mult = "1";
for (var i=0; i<zeros; i++)
mult += "0";
mult = parseInt(mult);
return parseFloat(Math.round(decimal*mult)/mult);
}
function App(form) {
var soc = roundedDecimal(form.soc.value,2);
var parb = roundedDecimal(form.parb.value,2);
var ind = roundedDecimal(form.ind.value,2);
var wage = roundedDecimal(form.wage.value,2);
var invest = roundedDecimal(form.invest.value,2);
var other = roundedDecimal(form.other.value,2);
var nurse = roundedDecimal(form.nurse.value,2);
var home = roundedDecimal(form.home.value,2);
var pre = roundedDecimal(form.pre.value,2);
var Total = soc + parb + ind + wage + invest + other;
var Deduct = nurse + home + pre;
var Newtotal = Total - Deduct;
form.total.value = Total;
form.deduct.value = Deduct;
form.newtotal.value = Newtotal;
}
Flakbait
09-12-2002, 06:14 PM
It's still throwing totals off with some number combinations. The really freakish part is, Deduct isn't changing. Only Total and Newtotal are getting pitched for some odd reason. I'm going to keep after this pesky critter and see if I can't track down the glitch.
Thanks again beetle! Sure do appreciate it
---------------
Flakbait
beetle
09-12-2002, 06:36 PM
One thing. I wouldn't used the word 'form' for your function variable. use 'f' or 'frm' or something else instead.
Can you send me a link to this page? Sounds like a strange error....
Flakbait
09-12-2002, 06:46 PM
Sure. The page is over here (http://www.worldaccessnet.com/~delta6/bugs/tax_calc.htm).
----------------
Flakbait
Slowing driven insane by code :D
beetle
09-12-2002, 06:51 PM
Oh, just run the roundedDecimal() function again on the totals before you output them to the form inputs
-OR-
use this currency function I've got (I didn't write it, but it works :D)function currency(anynum)
{
//-- Returns passed number as string in $xxx,xxx.xx format.
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}
//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}
//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval = dStr + pStr
//-- Put numbers in parentheses if negative.
if (anynum<0) {retval="("+retval+")"}
return "$"+retval
}
Flakbait
09-13-2002, 07:40 AM
Running the roundedDecimal() on each total was the answer! It's not throwing any numbers and it's dead on with every calculation! :D
Thanks again!
--------------------
Flakbait
Not calling the assylum just yet
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.