Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
06-03-2013, 11:46 AM #1
- Join Date
- Jun 2013
- Thanked 0 Times in 0 Posts
I made a calculator, but it doesn't do decimals and I'm not sure how to fix it.
Basically, I need to add a sort of shopping-cart calculator to the site. There's a list of products, and beside each product, a "Quantity" field. The calculator knows that Product X is $2.70 and Product Y is $4, so when the user hits the 'calculate total' button, it multiplies each product's price by its quantity, adds them all up, and gives a total figure.
That's it. Simple, right?
Well, here's what I've done so far. Here's the first section, which defines the Cal function.
<form> <table width="100%"> <tr><td>Product One</td> <td style="text-align:right;"><span class="55"> Qty:<input name="two" size="5" /></span></td></tr> <tr><td>Product Two</td> <td style="text-align:right;"><span class="84"> Qty:<input name="three" size="5" />/span></td></tr> <tr><td></td> <td style="text-align:right;"><input type="button" name="" value="Calculate Total" onmouseup="Cal(this,'two','three','total');" /> <input name="total" /></form></td></tr></table>
Now, what I've got there actually does work perfectly fine -- except that it doesn't calculate decimals, and that's a problem for me. Some of the prices need to be things like $3.70, and that calculator doesn't understand that.
My initial idea was to simply list all the prices in cents, and have the Cal function divide by 100 as its last step. But I'm not sure how to actually add that to the code -- nothing I've tried works.
Could anyone tip me off as to how I might do that? I'd be grateful.
06-03-2013, 12:53 PM #2
- Join Date
- Jun 2002
- London, England
- Thanked 2,564 Times in 2,542 Posts
Says delete any character which is not a digit (0-9). That will include a decimal point.
Change it to
which means delete any charcater not 0-9 or decimal point.
Quizmaster: Which pear-shaped organ stores bile?
Contestant: The vulva
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.