Connecting Tech Pros Worldwide Help | Site Map

Re: problem with javascript in asp not rounding up..

Adrienne Boswell
Guest
 
Posts: n/a
#1: Aug 22 '08
Gazing into my crystal ball I observed =?Utf-8?B?R1ROMTcwNzc3?=
<GTN170777@discussions.microsoft.comwriting in
news:443E2509-7F0E-4CEC-B243-D6EDC931DB7F@microsoft.com:

ASP has no knowledge of the client, this is a client side issue.
Followups set to comp.lang.javascript.
Quote:
Hi All,
>
I have a neat little script that calculates price based on quantity
without refreshing the page.. the script is -
>
><script type="text/javascript">
function OpenWin(url)
{
window.open(url,'win','scrollbars=1,status=0,resiz able=
0,widt
Quote:
h=200,height=265');
}
>
>
function btnCalculate_onclick()
{
var numQty;
var adprice;
>
if (isNaN(document.frmClient.txtQty.value))
{
alert('Please enter a number for advert quantity.');
}
else
{
numQty = parseInt(document.frmClient.txtQty.value);
adprice = parseInt(document.frmClient.adprice.value);
>
if((numQty >= 1) && (numQty <= 1)){
document.getElementById('divPrice').innerHTML = '£' + adprice + '
+ VAT'; document.getElementById('maindivPrice').innerHTML = '£' +
Math.round(100*(adprice * numQty))/100 + ' + VAT';
} else if((numQty >= 2) && (numQty <= 10)){
document.getElementById('divPrice').innerHTML = '£' +
(Math.round(adprice * numQty)*.95)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '£' +
Math.round(adprice * numQty)*.95 + ' + VAT';
} else if((numQty >= 11) && (numQty <= 20)){
document.getElementById('divPrice').innerHTML = '£' +
(Math.round(adprice * numQty)*.85)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '£' +
Math.round(adprice * numQty)*.85 + ' + VAT';
} else if((numQty >= 21) && (numQty <= 30)){
document.getElementById('divPrice').innerHTML = '£' +
(Math.round(adprice * numQty)*.75)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '£' +
Math.round(adprice * numQty)*.75 + ' + VAT';
} else if((numQty >= 31) && (numQty <= 50)){
document.getElementById('divPrice').innerHTML = '£' +
(Math.round(adprice * numQty)*.5)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '£' +
Math.round(adprice * numQty)*.5 + ' + VAT';
} else {
alert("Please contact us for more information about the benefits
of our
monthly account packages.");
}
}
}</script>
>
The problem I have is that it doesn't round up, for instance in stead
of £0.95 i get £0.9499999999998.
>
Does anyone have any ideas how to fix this?
>
Many thanks
>


--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

Dr J R Stockton
Guest
 
Posts: n/a
#2: Aug 22 '08

re: Re: problem with javascript in asp not rounding up..


In comp.lang.javascript message <Xns9B023D4525689arbpenyahoocom@69.16.18
5.247>, Fri, 22 Aug 2008 13:01:23, Adrienne Boswell <arbpen@yahoo.com>
posted:
Quote:
Quote:
>The problem I have is that it doesn't round up, for instance in stead
>of £0.95 i get £0.9499999999998.
>>
>Does anyone have any ideas how to fix this?
You could try reading the FAQ of comp.lang.javascript, and/or my site.

A programmer should know that binary floats can represent few simple
decimal fractions exactly.

If your results are hoped to be in pounds, with a 2-digit decimal
fraction part, then calculate in pence and insert the point as part of
output formatting.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Closed Thread


Similar JavaScript / Ajax / DHTML bytes