I call a function that takes the unit price and
quantity ordered to create an amount...it looks
something like this.
function calculateCost()
{
quantity=document.RFO.quantity.value;
unitPrice=document.RFO.unitPrice.value;
total=0;
The variables - quantity -, - unitPrice - and - total - should be
declared local to the function using the - var - keyword whenever there
is no good reason for them being global (and there doesn't appear to be
any reason for that).
if(isPositiveInteger(quantity))
{
quantity=parseFloat(quantity)
Why - parseFloat -? Surly a quantity has got to be an integer (and
particularly if - isPositiveInteger - does what its name suggests), so
why not - parseInt - and if you are verifying that the string contains a
representation of a positive integer why not let the type conversion
implicit in multiplication do that job.
{ ^
total=quantity * unitPrice;
}
^
What are these opening and closing braces doing here? They form a Block
statement, but there is no reason for a Block statement appearing here.
}
document.RFO.total.value=format(total)
}
if a person enters a unit price that ends with .10,
.20, .30, ..., .90, my code drops a penny off the
total? However if the price ends with .1, .2, .3,
..., .9, my code works fine. Seems it has a problem
calculating prices ending with 0 unless it's
.00
There is nothing in this code that would suggest the phenomenon you
describe. A quick test of the javascript operations you are using finds
no evidence of differing results using strings with trailing zeros and
the same strings with the trailing zeros stripped off:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<pre>
<script type="text/javascript">
var l = [
'1.00','1.10','1.20','1.30','1.40','1.50','1.60','1.70','1.80','1.90',
'2.00','2.10','2.20','2.30','2.40','2.50','2.60','2.70','2.80','2.90',
'3.00','3.10','3.20','3.30','3.40','3.50','3.60','3.70','3.80','3.90',
'4.00','4.10','4.20','4.30','4.40','4.50','4.60','4.70','4.80','4.90',
'5.00','5.10','5.20','5.30','5.40','5.50','5.60','5.70','5.80','5.90',
'6.00','6.10','6.20','6.30','6.40','6.50','6.60','6.70','6.80','6.90',
'7.00','7.10','7.20','7.30','7.40','7.50','7.60','7.70','7.80','7.90',
'8.00','8.10','8.20','8.30','8.40','8.50','8.60','8.70','8.80','8.90',
'9.00','9.10','9.20','9.30','9.40','9.50','9.60','9.70','9.80','9.90'
];
var q = [
'1','2','3','4','5','6','7','8','9'
];
function isPositiveInteger(){
return true;
}
function calculateCost2(x, y){
quantity = q[y];
unitPrice = l[x];
total=0;
var unitPrice2, total2;
if(isPositiveInteger(quantity)){
quantity = parseFloat(quantity)
{
total = quantity * unitPrice;
unitPrice2 = unitPrice.substring(0, (unitPrice.length - 1));
total2 = quantity * unitPrice2;
}
}
return (quantity+' * '+unitPrice+' = '+total+'\n'+quantity+' * '+
unitPrice2+' = '+total2+'\t\t'+
((total != total2)?'********** Differ **********':''));
}
for(var c = 0;c < q.length;++c){
for(var d = 0;d < l.length;++d){
document.write(calculateCost2(d, c)+'\n\n')
}
}
</script>
</per>
</body>
</html>
With no evidence of the phenomena it cannot be attributed. You will have
to create an example that actually shows this in action. It would be a
good idea to mention the browser you are using in case this is an
implementation specific bug (I used IE and Mozilla for testing the above
code).
(Incidentally, although the FAQ reference VK posted is relevant to the
issues of money calculations and number to formatted string
transformations, it cannot account for the symptoms you describe. VK
should generally be ignored, he does not know javascript at all and as a
result he cannot see what would be expected from executing javascript
code, and so is incapable of attributing effects to causes except by
(inevitably often wrong) guess-work.)
Richard.