473,735 Members | 2,072 Online

# Help ! Shopping Cart Problem

I am having a problem putting together a shopping cart with the below
script. Everything displays fine, adds totals fine, and works perfect
EXCEPT if you choose the 9.95 item #5 BY ITSELF the total displayed is
\$9.94 ! If you add ANYTHING else the total is correct, 9.95 plus
whatever you add. That is the only price in these samples that is
doing that. All the others display properly. If you change the 9.95 to
ANY other number it displays correct. If you put 9.95 in any other
position (Item number 1 instead of 5 etc. it does the same thing)

TIA

(This is the entire html page you can paste it and view in a browser
to see the problem with the 9.95 item.)

<script language="JavaS cript">
<!-- hide contents from old browsers

var Cost, Grand_Total;

function tally()
{
Cost = 0;
if (document.order form.Item1.chec ked) { Cost = Cost + 119.95;
}
if (document.order form.Item2.chec ked) { Cost = Cost + 75.00; }
if (document.order form.Item3.chec ked) { Cost = Cost + 14.95; }
if (document.order form.Item4.chec ked) { Cost = Cost + 50.00; }
if (document.order form.Item5.chec ked) { Cost = Cost + 9.95; }
if (document.order form.Item6.chec ked) { Cost = Cost + 4.95; }
if (document.order form.Item7.chec ked) { Cost = Cost + 50.00; }
if (document.order form.Item8.chec ked) { Cost = Cost + 7.95; }
if (document.order form.Item9.chec ked) { Cost = Cost + 1.00; }

Cost = dollar(Cost);
Grand_Total = parseFloat(Cost )
Grand_Total = dollar(Grand_To tal);

document.orderf orm.GrandTotal. value = "\$" + Grand_Total;
}

function dollar (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amou nt/100);
if (((amount) == Math.floor(amou nt)) && ((amount - Math.floor
(amount)) == 0))
{
amount = amount + ".00"
return amount;
}
if ( ((amount * 10) - Math.floor(amou nt * 10)) == 0)
{
amount = amount + "0";
return amount;
}
if ( ((amount * 100) - Math.floor(amou nt * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}

//-->
</script>
<body><center >

<form method="post" name="orderform " action="mailto: me@mydomain.com "
enctype="text/plain">
<table bgcolor="#EFEFE F" border="0" cellspacing="0"
cellpadding="3" ><font face="verdana" size="1" color="#000000" ><b>
<tr><td valign="top"><f ont face="verdana" size="2"
color="#000000" ><b>
<p><input name="Item1" value="Accelera tedHighSpeedAnn ual"
onclick="tally( )" type="checkbox" >Accelerated Dial-Up Internet Access
1 Year Pre-Pay (\$119.95)
</p><p><input name="Item2" value="Item2_ch osen" onclick="tally( )"
type="checkbox" > Accelerated Dial Up Internet Access Semi-Annual
Pre-Pay (\$75.00)
</p><p><input name="Item3" value="Item3_ch osen" onclick="tally( )"
type="checkbox" > Accelerated Dial Up Internet Access Monthly Billing
(\$14.95)
</p><p><input name="Item4" value="Item4_ch osen" onclick="tally( )"
type="checkbox" > Regular Dial Up Internet Access Semi-Annual Billing
(\$50.00)
</p><p><input name="Item5" value="Item5_ch osen" onclick="tally( )"
type="checkbox" > Regular Dial Up Internet Access Monthly Billing
(\$9.95)
</p></td><td valign="top"><f ont face="verdana" size="2"
color="#000000" ><b>
</p><p><input name="Item6" value="Item6_ch osen" onclick="tally( )"
type="checkbox" > Add 3 Email Monthly Billing (\$4.95)
</p><p><input name="Item7" value="Item7_ch osen" onclick="tally( )"
type="checkbox" > Add 3 Email 1 Year Pre-Pay (\$50.00)
</p><p><input name="Item8" value="Item8_ch osen" onclick="tally( )"
type="checkbox" > 10 Email Only Monthly Billing Only (7.95)
</p><p><input name="Item9" value="Item9_ch osen" onclick="tally( )"
type="checkbox" > Mail Invoice for Payment by Check (\$1.00)
<br><br><font color="red">Tot al Due: <input name="GrandTota l"
value="\$0" size="8" type="text"></font>
</p></td></tr></table><br><br>
<center><inpu t value="Send Order" type="submit">< input value="Reset
Order" type="reset"></center>
</form>
</body></html>
Oct 29 '05 #1
17 2344
Phil McKraken wrote:
I am having a problem putting together a shopping cart with the below
script. Everything displays fine, adds totals fine, and works perfect
EXCEPT if you choose the 9.95 item #5 BY ITSELF the total displayed is
\$9.94 ! If you add ANYTHING else the total is correct, 9.95 plus
whatever you add. That is the only price in these samples that is
doing that. All the others display properly. If you change the 9.95 to
ANY other number it displays correct. If you put 9.95 in any other
position (Item number 1 instead of 5 etc. it does the same thing)

The problem is your dollar() function, here is a link to some 'to money'
conversion stuff:

<URL:http://www.merlyn.demo n.co.uk/js-maths.htm#Money >

Or search the group for money conversion functions, I'm sure there are a
couple of good ones that have been posted. There is an extensive thread
here:

Converting strings to numbers and back using multiplication may result
in a rounded value that is not what you started with - 9.95 being a
'magic' number for your dollar function.

Use string manipulation for formatting numbers if you want to ensure
that they don't get modified.

[...]

--
Rob
Oct 29 '05 #2
The problem is your dollar() function, here is a link to some 'to money'
conversion stuff:

I tried the links you posted but, (: no luck. I am sure I am just
doing it wrong. I got this sample from one of those javascript web
repositories and I am not competent to change it to make it work. I am
still learning and am not really a js writer, only a "cut n paste"
trial & error modifier of what's there already.

Can someone tell me what to do to fix this a little more specifically.

Thanks for the responses. I really appreciate it. This is the way you
learn I guess.

When I tried the links, I screwed it up so bad it doesn't work at all
now. (Yes I have a backup that works the same as the original with the
"9.95" problem)

Thanks again.
Oct 29 '05 #3
Phil McKraken said the following on 10/29/2005 12:03 PM:
The problem is your dollar() function, here is a link to some 'to money'
conversion stuff:

I tried the links you posted but, (: no luck. I am sure I am just
doing it wrong. I got this sample from one of those javascript web
repositories and I am not competent to change it to make it work. I am
still learning and am not really a js writer, only a "cut n paste"
trial & error modifier of what's there already.

Can someone tell me what to do to fix this a little more specifically.

You are trying to round by multiplying by 100 and then dividing by 100.
That introduces the error you see because computers cannot represent
9.95 exactly in Base 2. So you get the error.

The way around that error is to convert your Number to a whole number,
Operations*. Meaning, you do not divide by 100, you convert it to a
String and then add the decimal.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 29 '05 #4
Phil McKraken said:

I am having a problem putting together a shopping cart with the below
script. Everything displays fine, adds totals fine, and works perfect
EXCEPT if you choose the 9.95 item #5 BY ITSELF the total displayed is
\$9.94 ! If you add ANYTHING else the total is correct, 9.95 plus
whatever you add. That is the only price in these samples that is
doing that. All the others display properly. If you change the 9.95 to
ANY other number it displays correct. If you put 9.95 in any other
position (Item number 1 instead of 5 etc. it does the same thing)

That's really just horrible code, and should be thrown away,
but the simplest fix to your problem is to remove the line
indicated below. Every time you call that ridiculous dollar()
function you introduce more error, so don't call it without
any reason. There's also no reason to use either parseFloat()
or parseInt() in this code, but at least they're not causing
the problem.

function tally()
{
Cost = 0;
if (document.order form.Item1.chec ked) { Cost = Cost + 119.95;
}
if (document.order form.Item2.chec ked) { Cost = Cost + 75.00; }
if (document.order form.Item3.chec ked) { Cost = Cost + 14.95; }
if (document.order form.Item4.chec ked) { Cost = Cost + 50.00; }
if (document.order form.Item5.chec ked) { Cost = Cost + 9.95; }
if (document.order form.Item6.chec ked) { Cost = Cost + 4.95; }
if (document.order form.Item7.chec ked) { Cost = Cost + 50.00; }
if (document.order form.Item8.chec ked) { Cost = Cost + 7.95; }
if (document.order form.Item9.chec ked) { Cost = Cost + 1.00; }

Cost = dollar(Cost); // REMOVE THIS LINE
Grand_Total = parseFloat(Cost )
Grand_Total = dollar(Grand_To tal);

document.orderf orm.GrandTotal. value = "\$" + Grand_Total;
}

Oct 29 '05 #5
On 29 Oct 2005 11:05:04 -0700, Lee <RE************ **@cox.net> wrote:
That's really just horrible code, and should be thrown away,
but the simplest fix to your problem is to remove the line
indicated below. Every time you call that ridiculous dollar()
function you introduce more error, so don't call it without
any reason. There's also no reason to use either parseFloat()
or parseInt() in this code, but at least they're not causing
the problem.

I appreciate the help and the sentiment regarding the code. I guess I
will have to find another one. I DID remove the line you suggested but
ALSO had to remove the "dollar" reference in the 2nd line down, with
Grand_Total.

Doing that caused the 9.95 and other **.95 items to display and add
ok. BUT, when you add ANY **.95 item to anything else (resulting in a
total that ends in "0" it drops off the "0".

I'm just not understanding enough of js yet to trouble shoot this.

Anyone know a better one ?

Thanks
Oct 29 '05 #6
JRS: In article <6s************ *************** *****@4ax.com>, dated
Sat, 29 Oct 2005 12:36:50, seen in news:comp.lang. javascript, Phil
McKraken <ti******@hotma il.com> posted :
I am having a problem putting together a shopping cart with the below
script. Everything displays fine, adds totals fine, and works perfect
EXCEPT if you choose the 9.95 item #5 BY ITSELF the total displayed is
\$9.94 ! If you add ANYTHING else the total is correct, 9.95 plus
whatever you add. That is the only price in these samples that is
doing that. All the others display properly. If you change the 9.95 to
ANY other number it displays correct.
You have tried all other numbers? For me, 19.65 gives 19.64.
If you put 9.95 in any other
position (Item number 1 instead of 5 etc. it does the same thing)

Read the newsgroup FAQ and what it cites.

Your conversion method, as well as being wrong, appears unnecessarily
large and slow. Delete your function "dollar"; euthanasia is the
kindest treatment for it.

Most scripts in Web repositories are trash; many are also bloatware.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 29 '05 #7
01.iinet.net.au >, dated Sat, 29 Oct 2005 23:44:47, seen in
news:comp.lang. javascript, RobG <rg***@iinet.ne t.au> posted :

The problem is your dollar() function, here is a link to some 'to money'
conversion stuff:

<URL:http://www.merlyn.demo n.co.uk/js-maths.htm#Money >

<URL:http://www.merlyn.demo n.co.uk/js-round.htm> is a more general
reference; and then there's the code in the FAQ.

--
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news. groups>.
Oct 29 '05 #8
Phil McKraken wrote:
The problem is your dollar() function, here is a link to some 'to money'
conversion stuff:

I tried the links you posted but, (: no luck. I am sure I am just
doing it wrong. I got this sample from one of those javascript web
repositories and I am not competent to change it to make it work. I am
still learning and am not really a js writer, only a "cut n paste"
trial & error modifier of what's there already.

Can someone tell me what to do to fix this a little more specifically.

You really need to understand what the functions are doing and how they
work, else you will get errors like you have now. I hope you are
validating everything back at the server and only using the shopping

Anyhow, here's a couple of functions that may suit. They do no
validation at all and expect you to pass them suitable input. The first
expects to get dollars and never more than two decimal places, the
second expects integer cents.

<script type="text/javascript">

// This function expects to get dollars as either an integer
// or a float but never more than 2 decimal places
function toDollars(amt)
{
if (0 == amt) return '0.00';
amt += ''; // Convert amt to string
amt = amt.split('.');
if (1 == amt.length) amt[1]='00';
if (0 == amt[1].length) amt[1] += '00';
if (1 == amt[1].length) amt[1] += '0';
return amt.join('.');
}

// This function expects to get only integer cents
function centsToDollars( amt)
{
amt += ''; // Convert amt to string
if (1 == amt.length) return '0.0'+ amt;
if (2 == amt.length) return '0.'+ amt;
var dollars = amt.substring(0 ,amt.length-2);
var cents = amt.substring(a mt.length-2);
return dollars + '.' + cents;
}

</script>
Play with toDollars<br>
<input type="text" onblur="
this.value = toDollars(this. value);
"><br>
Play with centsToDollars< br>
<input type="text" onblur="
this.value = centsToDollars( this.value);
">

</body></html>
[...]

--
Rob
Oct 30 '05 #9
JRS: In article <5v************ ********@comcas t.com>, dated Sat, 29 Oct
2005 13:45:09, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :

You are trying to round by multiplying by 100 and then dividing by 100.
That introduces the error you see because computers cannot represent
9.95 exactly in Base 2. So you get the error.

Non sequitur.

If the representation of 9.95, while not exact, had sufficient zeroes at
the end of the mantissa, then the multiplication would be exact, and the
division would be exact and would give the original value. There,
"sufficient zeroes" is a sufficient, but probably not entirely
necessary, condition.

If 9.95 being represented inexactly were the only criterion, then ISTM
that one would expect more wrong results than the OP's code actually
gives.
The real problem is that the OP's type of method effectively truncates
to cents using an inexact input; the input must be rounded to exact
cents, which has to be done into a representation where all such values
can be rendered exactly.

The ordinary conversions of a general Number to a String do that
rounding, as does the obvious one of the conversions to an integer Cent
number.

Note that one should always multiply or divide by 100 in preference to
dividing or multiplying by 0.01, since 100 is represented exactly but
0.01 is not.

--
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news. groups>.
Oct 30 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.