/    Sign up×
Community /Pin to ProfileBookmark

Problem with doing some javascript math on the fly

I am doing some calculations on the fly for my client, but the javascript keeps returning a nan when I put my numbers into the fields. On the front end I am using 2 labels (one they can not change, and other being the total cost. then there are 3 textboxes that can be manipulated to equate the correct total.

Any help would be greatly appreciated. BTY, programming in vb.net with javascript.

[CODE]function doCalcVendorFees(){

var el1=document.getElementById(“lblBaseFee”).innerHTML;
var el2=document.getElementById(“txtCopyFee”);
var el3=document.getElementById(“txtCertificationFee”);
var el4=document.getElementById(“txtOtherFee”);

var FeeTotal = parseFloat(el1.value + el2.value + el3.value + el4.value);
document.getElementById(“lblFeeTotal”).innerHTML = FeeTotal;

}[/CODE]

Thank you in advance.

CommDown

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJun 30.2005 — el1.value + el2.value + el3.value + el4.value

these are all strings

each one needs to be changed to a numer before adding
Copy linkTweet thisAlerts:
@CommDownauthorJul 01.2005 — OK, I have made sure that they are all numbers now, but I am still getting NaN, maybe I am missing something. JavaScript, by far, is not my strong point. Thanks for any help!

[CODE]function doCalcVendorFees(){

var el1 = document.getElementById("lblBaseFee").innerHTML;
var el2 = document.getElementById("txtCopyFee").value;
var el3 = document.getElementById("txtCertificationFee").value;
var el4 = document.getElementById("txtOtherFee").value;
var FeeTotal = 0.0;

if (el1.length < 1) {
FeeTotal += parseFloat(el1);
}
if (el2.length < 1) {
FeeTotal += parseFloat(el2);
}
if (el3.length < 1) {
FeeTotal += parseFloat(el3);
}
if (el4.length < 1) {
FeeTotal += parseFloat(el4);
}

alert(FeeTotal)
document.getElementById("lblFeeTotal").innerHTML = FeeTotal.toString();

}[/CODE]


I added the if statements to make sure that if the fields are empty, it calculates accordingly.

Thanks

CommDown
Copy linkTweet thisAlerts:
@ccoderJul 01.2005 — The only time you increment FeeTotal is when a field is empty (.length < 1). Change the test to .length > 0.
Copy linkTweet thisAlerts:
@CommDownauthorJul 01.2005 — Thank you, that was a simple overlook... one last question, if I have half dollar amounts (ie. 4.50) it drops the last zero so it would be 4.5 how can I get that to carry the two decimal points?
Copy linkTweet thisAlerts:
@ccoderJul 01.2005 — Here is some code that I found somewhere (I should probably make a note of these things so I can give credit to the proper sources).
<i>
</i> function format(expr, decplaces)
{
// raise incoming value by power of 10 times the
// number of decimal places; round to an integer; convert to string
var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces))

<i> </i> // pad small value strings with zeros to the left of rounded number
<i> </i> while (str.length &lt;= decplaces)
<i> </i> str = "0" + str

<i> </i> // establish location of decimal point
<i> </i> var decpoint = str.length - decplaces

<i> </i> // assemble final result from: (a) the string up to the position of
<i> </i> // the decimal point; (b) the decimal point; and (c) the balance
<i> </i> // of the string. Return finished product.

<i> </i> return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);

<i> </i> } // end of format
×

Success!

Help @CommDown spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...