|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey there!
Ok, Since I posted my last querry, I have had a lot of progress with my script. I still have to make a few changes to it. 1 - I am still trying to make the calc. automatically calculate the price as soon as you type in the quantity. 2 - I am also having a problem with the price. The set price is $30.90, but when you type in a quantity of 3, 6, or 9 it is $.01 off. If anyone can help I would appreciate it. Here is the script that I have <script LANGUAGE="JavaScript"> function dp(price) { string = "" + price; number = string.length - string.indexOf('.'); if (string.indexOf('.') == -1) return string + '.01'; if (number == 1) return string + '00'; if (number == 2) return string + '0'; if (number > 3) return string.substring(0,string.length-number+3); return string; } function calculate() { document.calcform.total1.value = dp((document.calcform.price1.value)*(document.calcform.quantity1.value)) } </script> </head> <body bgcolor="#FFFFFF" onload="calculate"> <center><form name="calcform" action="http://www./mailto:YOU@YOURSITE.COM" method="post"></center> <center><table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="600" > <tr> <td>QUANTITY</td> <td>PRICE</td> <td>TOTAL</td> </tr> <tr> <td><input type="text" name="quantity1" size="5" ONCHANGE="calculate()"></td> <td>$30.90/ea<input type=hidden name="price1" value="30.90" ONCHANGE="calculate()"></td> <td><input type="text" name="total1" size="10"></td> </tr> <tr> <td></td> <td><input type="button" Value="Calculate" ONCLICK="calculate()"></td> <td></td> </tr> </table></center> <center></form></center> </body> </html> |
|
#2
|
|||
|
|||
|
I had the same problem with my java calculator. The unit price was $9.95, but multiples of 3, 6 and 7 caused the total to end in .9999999 - always .0000001 shy of being correct.
I played with it for a while and finally suggested to the client that we price the product at an even $10 rather than $9.95. I hated doing that. It felt like DEFEAT! If someone can explain why it does this, I would really like to know. Thanks. |
|
#3
|
|||
|
|||
|
1. your ONCHANGE event is case sensitive. change it to onChange and it will probably work
2. as for your math calc problem. I can't tell you why it happens accept that it is a computer. an easy fool proof solution would be: num=Math.round(num*100)/100; that would shift the cents to an integer, round up or down (in this case up) to the nearest integer, and then shift back to cents. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Javascript Order Calculator |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|