473,805 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)

Any ideas ? I am completely stumped. Please Help
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.)

<html><head>

<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>
</head>
<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 2350
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)

Any ideas ? I am completely stumped. Please Help


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:

<URL:http://groups.google.c om/group/comp.lang.javas cript/browse_frm/thread/9e52a2000323701 0/df3038f5a093443 6?tvc=1&q=conve rt+to+money&hl= en#df3038f5a093 4436>
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,
do your calculations, and then add the decimal back with *String
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
Lee
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)

Any ideas ? I am completely stumped. Please Help
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)

Any ideas ? I am completely stumped. Please Help


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
JRS: In article <43637d2f$0$217 05$5a62ac22@per-qv1-newsreader-
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
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
cart for advice to customers.

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.

<html><head><ti tle>Play</title>

<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>
</head><body>
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
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.

Similar topics

3
3273
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
2
2870
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as im going. Just a couple of hints will do, Im familiar with vb script but not java based code , and im wondering how to store what they select so I can move around different product ranges. Don
1
3584
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not be able to come along and add it to their shopping cart. thanks joy
1
1823
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
1
3232
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID in table. All products and cart status pages are on non SSL connection. On checkout to get secure user information I shifted connection to SSL but when shifting to SSL, the SessionID changed (As is this is default behavior of IIS to prevent...
2
2301
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic screen generation routines (already written) that take an XML stream as input from various "search for products" forms. That way I can run queries in one window and display the dynamic results in another. The searching functions will probably
7
2639
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would like it to work properly so that a record count counts through all the books and genertates these numbers. watch out for line breaks <%@ Language=VBScript %> <% Option Explicit %> <!--#include file="DatabaseConnect.asp"-->
2
2324
by: isaac2004 | last post by:
hi i am creating a basic asp site that uses cookies to manage a cart for an online store. whenever i open this page without adding anything to the cart. i get an error message. here is my code <%@ Language=VBScript %> <% Option Explicit %> <!--#include file="DatabaseConnect.asp"--> <!--#include virtual="06winter/levini/database/adovbs.inc"-->
3
1552
by: isaac2004 | last post by:
hello i am making a spoof online book store site for a class and I was wondering how i could fix a problem i am having. I have two tables, one the cart and the other a table with book descriptions. what i am trying to do is display the book info for the cart through a SQL statement. the only problem is when i do this it just outputs the info for all books in the database. how would i change my SQL statement to only read the info for the...
0
3698
by: samjam | last post by:
Below is some coding in a program i am using, i would like to know how i can get the text bigger or bolder on my webpage, This is the section of text i would like bigger or bolder (This is a very rare lacquered tea caddy c1840. The outside of the caddy has wonderful scenes on each side which are really finely painted. The caddy stands on claw feet. Inside there are four tin canister which is very rare to see on such a small caddy. All canisters...
0
10607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.