Velocity Reviews > HTML > Cost Calculation Function With Parsed Numbers to Currency: Need Help Combining Functions

Cost Calculation Function With Parsed Numbers to Currency: Need Help Combining Functions

Neredbojias
Guest
Posts: n/a

 10-03-2005
With neither quill nor qualm, Fred quothed:

> I'm trying to validate this script so the number entered is parsed
> and a dollar sign is automatically entered before the number onBlur.
>
> My problem is that when I do this, the script interoperates the dollar
> sign as a character and not a numerical number and the script crashes
> (NaN).

It should be easy to strip the \$ before processing, no??

--
Neredbojias
Contrary to popular belief, it is believable.

Fred
Guest
Posts: n/a

 10-03-2005
I'm trying to validate this script so the number entered is parsed
and a dollar sign is automatically entered before the number onBlur.

My problem is that when I do this, the script interoperates the dollar
sign as a character and not a numerical number and the script crashes
(NaN).

FYI: When I tested it, I executed the funtion via a double onBlur
call from the text box.

My best guess to resolving the issue is to insert the function within
the script. How would I go about doing this with out crashing
function calc()}?

// Here's the working calculation functions - - - - - -
// Here's the working calculation functions - - - - - -
// Here's the working calculation functions - - - - - -

function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
one = document.autoSumForm.firstBox.value;
two = document.autoSumForm.secondBox.value;
three = document.autoSumForm.thirdBox.value;
four = document.autoSumForm.fourthBox.value;
five = document.autoSumForm.fithBox.value;
six = document.autoSumForm.sixthBox.value;
var totalCost = (one * 1) + (two * 1) + (three * 1) + (four * 1) +
(five * 1) + (six * 1);
document.autoSumForm.seventhBox.value = totalCost;
var fee = document.autoSumForm.TotalServiceFee.value;
if ( fee != "" ) {
var monthlySavings = totalCost - parseFloat(fee);
var yearlySavings = monthlySavings * 12;
document.autoSumForm.TotalMonthlySavings.value =
monthlySavings;
document.autoSumForm.TotalYearlySavings.value = ("" +
yearlySavings).replace(/(\.[0-9]{2}).*\$/g, "\$1");
}

}
function stopCalc(){
clearInterval(interval);
}
calc();

}

// Here's the working parse number to currency functions - -
// Here's the working parse number to currency functions - -
// Here's the working parse number to currency functions - -

var prefix="\$"
var wd

function parseelement(thisone){
if (thisone.value.charAt(0)=="\$")
return
wd="w"
var tempnum=thisone.value
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d"
break
}

}
if (wd=="w")
thisone.value=prefix+tempnum+".00"
else{

if (tempnum.charAt(tempnum.length-2)=="."){
thisone.value=prefix+tempnum+"0"
}
else{
tempnum=Math.round(tempnum*100)/100
thisone.value=prefix+tempnum
}
}
}

How do I merge these two???

Fred
Guest
Posts: n/a

 10-03-2005

>
>It should be easy to strip the \$ before processing, no??

I was thinking after processig.

How would you do it? Can you post an example?

Neredbojias
Guest
Posts: n/a

 10-04-2005
With neither quill nor qualm, Fred quothed:

>
> >
> >It should be easy to strip the \$ before processing, no??

>
>
> I was thinking after processig.
>
> How would you do it? Can you post an example?

Well, in javascript it'd be simply:

var a;
....
....
a=a.slice(1);

(You could also use regexp replace.)

If you have to verify whether or not the "\$" is there:

if (a.slice(0,1)=="\$") {
a=a.slice(1);
}

There's similar functions in php, etc.

--
Neredbojias
Contrary to popular belief, it is believable.