"GTN170777" <GTN170777@discussions.microsoft.comwrote in message
news:443E2509-7F0E-4CEC-B243-D6EDC931DB7F@microsoft.com...
Quote:
Hi All,
>
I have a neat little script that calculates price based on quantity
without
Quote:
refreshing the page.. the script is -
>
<script type="text/javascript">
function OpenWin(url)
{
>
window.open(url,'win','scrollbars=1,status=0,resiz able=0,width=200,height=26
5');
Quote:
}
>
>
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';
Quote:
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
Quote:
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?
The short answer is do not concatenate a number directly in to a string use
the Number object's .toFixed method.
var n = 250 * 0.83
var s = n.toFixed(2)
However while I'm here have you noticed that the code in each of your Ifs
are identical except for that multiplier value changes? This pattern should
cause you to see a need for a function the can get the multiplier value. In
the code below I've refactored out a function which returns the discount for
a specified quantity. Note that by returning the actual discount in this
function it makes the relationship between the quantity and the discount
given much clearer.
I've made a business decision (which clearly you may disagree with) to
discount the unit price then multiply the quantity by the discounted price.
This is less likely to be disputed by a user IMO.
I use Math.round to ensure the discounted price is to the nearest penny.
That still doesn't eliminate the need to use .toFixed since Numbers are
stored as floating point values which cannot exactly represent numbers
exactly only to a reasonable degree of precision.
function btnCalculate_onclick()
{
var numQty = parseInt(document.frmClient.txtQty.value);
var adprice = parseInt(document.frmClient.adprice.value);
if (isNaN(numQty))
{
alert('Please enter a number for advert quantity.');
}
else
{
var multiplier = null;
try { multiplier = 1 - getDiscount(numQty) }
catch(e) { alert(e); }
if (multiplier)
{
var discountedUnitPrice = Math.round(adprice * multiplier * 100) / 100
document.getElementById('divPrice').innerHTML = '£' +
discountedUnitPrice.toFixed(2) + '+ VAT';
document.getElementById('maindivPrice').innerHTML = '£' +
(discountedUnitPrice * numQty).toFixed(2) + ' + VAT';
}
}
}
function getDiscount(numQty)
{
if (numQty <= 1) return 0
if (numQty <= 10) return 0.05
if (numQty <= 20) return 0.15
if (numQty <= 30) return 0.25
if (numQty <= 50) return 0.5
throw "Please contact us for more information about the benefits of our
monthly account packages."
}
--
Anthony Jones - MVP ASP/ASP.NET