mrferrari
asked on
Javascript subtraction help needed
I can't see to figure this one out... yet it seems so simple
when I calculate the following equation I am getting a very out results 5.21-5 = .2099999999999999998.
I need it to compute the actual value which is .2100000
function ComputeChange5(Start, Change, End) {
document.MainForm[Change]. value = formatNum(eval(5.21-5),"^# ,###.5^");
}
I am looking for a very simple solution... I apss into the function the start and end values, and then look for the change value (which is the subtraction). I tried to simplify the code above after I narrowed down that its not my passed in variables, its simply the calucation. thx.
when I calculate the following equation I am getting a very out results 5.21-5 = .2099999999999999998.
I need it to compute the actual value which is .2100000
function ComputeChange5(Start, Change, End) {
document.MainForm[Change].
}
I am looking for a very simple solution... I apss into the function the start and end values, and then look for the change value (which is the subtraction). I tried to simplify the code above after I narrowed down that its not my passed in variables, its simply the calucation. thx.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The toFixed() function will round your number to the number of decimal places that you want. I am not too sure why you need to use eval() either.
ASKER
this is my full function
function ComputeChange5(Start, Change, End) {
var vartotal = 0;
var varname = "";
var FieldName = End;
if (document.MainForm[FieldNa me].value. replace(/\ ,/g,"") == ''|| isNaN(document.MainForm[Fi eldName].v alue.repla ce(/\,/g," "))){
document.MainForm[FieldNam e].value = formatNum(eval(document.Ma inForm.old varstorage .value.rep lace(/\,/g ,"")),"^#, ###.5^");
alert('You entered an invalid number')
document.MainForm[FieldNam e].focus() ;}
else
{
if (document.MainForm[End].va lue == document.MainForm.oldvarst orage.valu e)
{}
else
{
vartotal = numbertofixed(eval(documen t.MainForm [End].valu e.replace( /\,/g,"")) ,5) - eval(document.MainForm[Sta rt].value. replace(/\ ,/g,""));
document.MainForm[End].val ue = formatNum(eval(document.Ma inForm[End ].value)," ^#,###.5^" );
document.MainForm[Change]. value = formatNum(eval(vartotal)," ^#,###.5^" );
}
}
}
function ComputeChange5(Start, Change, End) {
var vartotal = 0;
var varname = "";
var FieldName = End;
if (document.MainForm[FieldNa
document.MainForm[FieldNam
alert('You entered an invalid number')
document.MainForm[FieldNam
else
{
if (document.MainForm[End].va
{}
else
{
vartotal = numbertofixed(eval(documen
document.MainForm[End].val
document.MainForm[Change].
}
}
}
ASKER
thanks...
I just change the last statement formatNum(eval(vartotal)," ^#,###.5^" );
to vartotal.toFixed(5)
any other suggestion on the function would be helpful, I am very new to java and frankly don;t use it much except these calculation functions... here and there.
thanks, enjoy your day.
I just change the last statement formatNum(eval(vartotal),"
to vartotal.toFixed(5)
any other suggestion on the function would be helpful, I am very new to java and frankly don;t use it much except these calculation functions... here and there.
thanks, enjoy your day.
There are parts of your function that seem repetitive or useless. For instance, what is the purpose of the FieldName variable?
Also, instead of
if (document.MainForm[FieldNa me].value. replace(/\ ,/g,"") == ''|| isNaN(document.MainForm[Fi eldName].v alue.repla ce(/\,/g," "))){
if may be easier to use
var num = parseFloat(document.MainFo rm[End].va lue.replac e(/,/g, ''));
if (isNaN(num)) {
Also,
if (document.MainForm[End].va lue == document.MainForm.oldvarst orage.valu e)
{}
does not seem necessary. It does not hurt to format the number again if it is the old one.
Also, instead of
if (document.MainForm[FieldNa
if may be easier to use
var num = parseFloat(document.MainFo
if (isNaN(num)) {
Also,
if (document.MainForm[End].va
{}
does not seem necessary. It does not hurt to format the number again if it is the old one.
ASKER
cool. super thanks. look at it now...
note, I have another function which is compute change. It is virtually the same code, but I just changed field name, that is why I left it in there at the top
function ComputeChange5(Start, Change, End) {
var vartotal = 0;
var FieldName = End;
var Num = parseFloat(document.MainFo rm[FieldNa me].value. replace(/\ ,/g,""))
if (isNaN(Num))
{
document.MainForm[FieldNam e].value = formatNum(eval(document.Ma inForm.old varstorage .value.rep lace(/\,/g ,"")),"^#, ###.5^");
alert('You entered an invalid number')
document.MainForm[FieldNam e].focus() ;}
else
{
vartotal = eval(document.MainForm[End ].value.re place(/\,/ g,"")) - eval(document.MainForm[Sta rt].value. replace(/\ ,/g,""));
document.MainForm[End].val ue = formatNum(eval(document.Ma inForm[End ].value)," ^#,###.5^" );
document.MainForm[Change]. value = vartotal.toFixed(5);
}
}
note, I have another function which is compute change. It is virtually the same code, but I just changed field name, that is why I left it in there at the top
function ComputeChange5(Start, Change, End) {
var vartotal = 0;
var FieldName = End;
var Num = parseFloat(document.MainFo
if (isNaN(Num))
{
document.MainForm[FieldNam
alert('You entered an invalid number')
document.MainForm[FieldNam
else
{
vartotal = eval(document.MainForm[End
document.MainForm[End].val
document.MainForm[Change].
}
}
ASKER
sad is I also have a function
function ComputeChange2(Start, Change, End)
which just replaces all the 5's with 2's and return 2 deciaml place results...
it was easier for me to copy the code that incorporate the logic into the function and pass in another parameter... looking back it seems lazy to me... im guilty and will fix it.
thanks again for the advice.
Marc
function ComputeChange2(Start, Change, End)
which just replaces all the 5's with 2's and return 2 deciaml place results...
it was easier for me to copy the code that incorporate the logic into the function and pass in another parameter... looking back it seems lazy to me... im guilty and will fix it.
thanks again for the advice.
Marc
ASKER
should I even be using eval and the format num?
how do I make the formatnum parameter, ,"^#,###.5^" dynamic, so I can pass in either a 2 or 5 to specify how many decimals I want...
I would like to award you some more points....
Marc
how do I make the formatnum parameter, ,"^#,###.5^" dynamic, so I can pass in either a 2 or 5 to specify how many decimals I want...
I would like to award you some more points....
Marc
It depends on how formatnum is coded. In fact, it may be easier to take out the .5 part of the format number and just use toFixed() explicitly.
If you want, you can always use something such as:
"^#,###." + VariableHoldingDecimalPlac es + "^"
Finally, you do need the majority of your eval() functions. You only need that if you have complex Javascript as a string and need to run it.
If you want, you can always use something such as:
"^#,###." + VariableHoldingDecimalPlac
Finally, you do need the majority of your eval() functions. You only need that if you have complex Javascript as a string and need to run it.
That effect is stumping already others, but what do you want to ahieve?
Do you need help for that formatNum() function?
Do you know the method toFixed(2)
Like this:
alert((5.21-5).toFixed(2))