Javascript calc in asp.net page wrong

D

David C

I have a Javascript function in my aspx page that gives me a different
answer than my calculator. For example, using a price of 415,000.00 and a
Size of 11,200 in my 2 controls I am trying to calculate the price per sq
ft. My Javascript comes up with 37.73 and my calculator comes up with 37.05
and i don't know why. Can anyone help? Below is my Javascript that
references these controls on my aspx page:

function calcpriceper()

{

var otxtsaleprice = document.getElementById('fvProperty_txtSalePrice');

var otxttotalsize = document.getElementById('fvProperty_txtTotalSize');

if (otxtsaleprice.value != '' && otxttotalsize.value != '')

{

var pricepersqft = (parseFloat(otxtsaleprice.value) /
parseFloat(otxttotalsize.value));

//alert('Price/Sq Ft = ' + Math.round(pricepersqft*100)/100);

var osq = document.getElementById('fvProperty_txtsqftprice');

osq.value = (Math.round(pricepersqft*100)/100);

}

}

David
 
J

Jesse Houwing

Hello David,
I have a Javascript function in my aspx page that gives me a different
answer than my calculator. For example, using a price of 415,000.00
and a Size of 11,200 in my 2 controls I am trying to calculate the
price per sq ft. My Javascript comes up with 37.73 and my calculator
comes up with 37.05 and i don't know why. Can anyone help? Below is
my Javascript that references these controls on my aspx page:

function calcpriceper()

{

var otxtsaleprice =
document.getElementById('fvProperty_txtSalePrice');

var otxttotalsize =
document.getElementById('fvProperty_txtTotalSize');

if (otxtsaleprice.value != '' && otxttotalsize.value != '')

{

var pricepersqft = (parseFloat(otxtsaleprice.value) /
parseFloat(otxttotalsize.value));

//alert('Price/Sq Ft = ' + Math.round(pricepersqft*100)/100);

var osq = document.getElementById('fvProperty_txtsqftprice');

osq.value = (Math.round(pricepersqft*100)/100);

}

}

David

I pasted this into the firefox javascript console and it comes up with the
expected value.

alert(Math.round(('415000'*1)/('11200'*1)*100)/100);

What are the contents of your parseFloat, method?
 
D

David C

The parseFloat returns 415 and 11. It removed the comma and everything
after. How can I fix this? Thanks.

David
 
M

Mark Rae [MVP]

D

David C

I used the replace method to replace comma with '' and it worked fine.
Thanks.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top