Advertisement

04.26.2008 at 10:07AM PDT, ID: 23356003
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.8

JavaScript calculator rounding problem, Internet Explorer, Firefox (latest versions)

Asked by ndechiaro in JavaScript

Tags: , ,

Hello,
I am trying to create a pricing calculator to be used on an html page, and I found a small
piece of JavaScript code which I used as the basis for my script.  The "Annual Cost" column
has a problem rounding for the "Blackberry devices" field and the "Totals" field.  An
example would be if you enter 1 in all the "quantity" fields, the product for the above
mentioned fields is 204.39 and 425.78 instead of the desired result of 204.40 and 425.80.  
I believe the problem is the "function dp(price)" portion of the script, and the value of
the Blackberry devices being 12.95.  The problem does not occur if I change the value to
12.50.
Requested but not required:
If possible, I would also like to add the following capabilities to the script:
1.) Add "$" sign before each amount generated in the "total" fields.
2.) Have a portion of the script dedicated to pricing, and use variable substitution to
enable a more efficient and accurate means of altering prices when needed.

Any suggestions would be greatly appreciated.
Thanks in advance for your assistance,
Nick

Here is the code:Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
<!-- This script has been in the http://www.javascripts.com Javascript Public Library! -->
<HTML><HEAD><TITLE>Pricing Calculator</TITLE>
<SCRIPT>
 
function dp(price) 
{
   string = "" + price;
   number = string.length - string.indexOf('.');
   if (string.indexOf('.') == -1)
      return string + '.00';
   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((13.50)*(document.calcform.quantity1.value))
document.calcform.total2.value = dp((12.95+49)*(document.calcform.quantity2.value))
document.calcform.total3.value = dp((4.95)*(document.calcform.quantity3.value))
document.calcform.subtotal.value = dp(eval(document.calcform.total1.value) + 
 
eval(document.calcform.total2.value) + eval(document.calcform.total3.value))
 
document.calcform.total11.value = dp((13.50)*(document.calcform.quantity1.value))
document.calcform.total21.value = dp((12.95)*(document.calcform.quantity2.value))
document.calcform.total31.value = dp((4.95)*(document.calcform.quantity3.value))
document.calcform.subtotal11.value = dp(eval(document.calcform.total11.value) + 
 
eval(document.calcform.total21.value) + eval(document.calcform.total31.value))
 
document.calcform.total12.value = dp((13.50*12)*(document.calcform.quantity1.value))
document.calcform.total22.value = dp((12.95*12+49)*(document.calcform.quantity2.value))
document.calcform.total32.value = dp((4.95*12)*(document.calcform.quantity3.value))
document.calcform.subtotal12.value = dp(eval(document.calcform.total12.value) + 
 
eval(document.calcform.total22.value) + eval(document.calcform.total32.value))
}
 
function resetBtn(){
document.calcform.quantity1.value=""
document.calcform.quantity2.value=""
document.calcform.quantity3.value=""
document.calcform.total1.value=""
document.calcform.total2.value=""
document.calcform.total3.value=""
document.calcform.subtotal.value=""
 
document.calcform.total11.value=""
document.calcform.total21.value=""
document.calcform.total31.value=""
document.calcform.subtotal11.value=""
 
document.calcform.total12.value=""
document.calcform.total22.value=""
document.calcform.total32.value=""
document.calcform.subtotal12.value=""
 
}
 
</SCRIPT>
</HEAD><BODY bgcolor="#ffffff" onload="calculate">
 
<FORM name="calcform">
 
<h2>Pricing Calculator Sample</h2>
<table border="1" width="880">
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">First Month*</td>
		<td align="center" width="106">Monthly Cost</td>
		<td align="center" width="106">Annual Cost</td>
	</tr>
	<tr>
		<td width="416">
		<p align="right">Please enter number of mailboxes required:</td>
		<td width="43" align="center"> 
<INPUT type="text" name="quantity1" size="6"></td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">
		<INPUT type="text" name="total1" size="14"></td>
		<td align="center" width="106"> 
		<INPUT type="text" name="total11" size="14"></td>
		<td align="center" width="106"> 
		<INPUT type="text" name="total12" size="14"></td>
	</tr>
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
	</tr>
	<tr>
		<td width="416">
		<p align="right">Please enter number of Blackberry devices:</td>
		<td width="43" align="center"> 
<INPUT type="text" name="quantity2" size="6"></td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">
		<INPUT type="text" name="total2" size="14"></td>
		<td align="center" width="106"> 
		<INPUT type="text" name="total21" size="14"></td>
		<td align="center" width="106"> 
		<INPUT type="text" name="total22" size="14"></td>
	</tr>
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
	</tr>
	<tr>
		<td width="416">
		<p align="right">Please enter number of Windows Mobile devices:</td>
		<td width="43" align="center">
		<INPUT type="text" name="quantity3" size="6"></td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">
		<INPUT type="text" name="total3" size="14"></td>
		<td align="center" width="106">
		<INPUT type="text" name="total31" size="14"></td>
		<td align="center" width="106">
		<INPUT type="text" name="total32" size="14"></td>
	</tr>
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">________</td>
		<td align="center" width="106">________</td>
		<td align="center" width="106">________</td>
	</tr>
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">
		<p align="right">Totals:</td>
		<td align="center" width="88">
		<INPUT type="text" name="subtotal" size="14"></td>
		<td align="center" width="106">
		<INPUT type="text" name="subtotal11" size="14"></td>
		<td align="center" width="106">
		<INPUT type="text" name="subtotal12" size="14"></td>
	</tr>
	<tr>
		<td width="416">&nbsp;</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
	</tr>
	<tr>
		<td width="416">* Includes one-time setup fees (if applicable)</td>
		<td width="43" align="center">&nbsp;</td>
		<td width="104">&nbsp;</td>
		<td align="center" width="88">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
		<td align="center" width="106">&nbsp;</td>
	</tr>
</table>
<p>Click this button to calculate your totals:&nbsp;&nbsp;&nbsp;
<INPUT type="button" Value="Calculate" ONCLICK="calculate()" name="btn1">&nbsp;&nbsp;&nbsp;
<INPUT type="button" Value="Reset" name="btn2" ONCLICK="resetBtn()"></p>
</FORM>
 
</BODY></HTML>
<!-- Pricing Calculator -->
[+][-]04.26.2008 at 11:17AM PDT, ID: 21446353

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 11:25AM PDT, ID: 21446385

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 11:36AM PDT, ID: 21446440

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 11:39AM PDT, ID: 21446460

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 01:03PM PDT, ID: 21446677

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 03:24PM PDT, ID: 21446979

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 04:05PM PDT, ID: 21447072

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 04:27PM PDT, ID: 21447130

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2008 at 08:35PM PDT, ID: 21448027

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 03:50AM PDT, ID: 21452797

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 04:07AM PDT, ID: 21452881

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: JavaScript, Internet Explorer, Firefox (latest versions), Not published at this time
Sign Up Now!
Solution Provided By: HonorGod
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.28.2008 at 09:23AM PDT, ID: 21455376

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 10:24AM PDT, ID: 21455801

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 10:48AM PDT, ID: 21456001

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 11:27AM PDT, ID: 21456345

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 11:48AM PDT, ID: 21456540

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:03PM PDT, ID: 21456662

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:04PM PDT, ID: 21456672

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:16PM PDT, ID: 21456779

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:26PM PDT, ID: 21456862

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:28PM PDT, ID: 21456873

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:30PM PDT, ID: 21456897

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:32PM PDT, ID: 21456922

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:56PM PDT, ID: 21457119

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.29.2008 at 07:11AM PDT, ID: 21462419

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628