Solved

How do I automatically round a percentage to 2 decimals places in a form?

Posted on 2007-12-05
3
1,893 Views
Last Modified: 2008-02-01
Hi,

I have a form with several input fields that sum up a total as a user inputs them and at the same time is calculating an average percentage in the last field. As a user inputs a number in each field the percentage field changes but it doesn't show it rounding off to 2 decimal places.

It will only round off to two decimal places when the user has tabbed through all of the fields, and  then when they reach the submit button.

Is there a way to always display the percentage rounded off to 2 decimal places instantly, without having to go to the end of the form?

Below is the code for the form:
<html>
<head>
</head>
 
<body>
 
<script type="text/javascript">
  function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  sun = document.myform.edit1.value;
  mon = document.myform.edit2.value;
  tues = document.myform.edit3.value;
  wed = document.myform.edit4.value;
  thurs = document.myform.edit5.value;
  fri = document.myform.edit6.value;
  sat = document.myform.edit7.value;
  document.myform.edit13.value = (sun * 1) + (mon * 1) + (tues * 1) + (wed * 1) + (thurs * 1) + (fri * 1) + (sat * 1);
  
  total = document.myform.edit13.value;
  quota = document.myform.edit20.value;
  document.myform.edit14.value = (total / quota * 100);
}
function stopCalc(){
  clearInterval(interval);
}
</script>
 
<script type="text/javascript">
/*<![CDATA[*/
function pcent(ele) {
var percent = document.getElementById(ele).value.replace('%','');
document.getElementById(ele).value = Number(percent).toFixed(2) + '%';
}
/*]]>*/
</script>
 
<form name="myform" method="post" action="index.php" onsubmit="return check(this)">
 
<br />
 
 
		<table width='90%' border='0' cellpadding='0' cellspacing='0'>
			
			<tr>
 
				<td class='label'><label>Sun:</label></td>
 
				<td><input type="text" name="edit1" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label></label></td>
			</tr>
			<tr>
				<td class='label'><label>Mon:</label></td>
 
				<td><input type="text" name="edit2" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
 
				<td class='label'><label></label></td>
			</tr>
			<tr>
				<td class='label'><label>Tues:</label></td>
 
				<td><input type="text" name="edit3" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label></label></td>
			</tr>
			<tr>
 
				<td class='label'><label>Wed:</label></td>
 
				<td><input type="text" name="edit4" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label></label></td>
			</tr>
			<tr>
				<td class='label'><label>Thurs:</label></td>
 
				<td><input type="text" name="edit5" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
 
				<td class='label'><label></label></td>
			</tr>
			<tr>
				<td class='label'><label>Fri:</label></td>
 
				<td><input type="text" name="edit6" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label for='blank'></label></td>
			</tr>
			<tr>
 
				<td class='label'><label>Sat:</label></td>
 
				<td><input type="text" name="edit7" onFocus="startCalc();" onBlur="stopCalc();" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label for='blank'></label></td>
			</tr>
 
			<tr><!--//begin total-->
 
				<td class='label'><label for='total'>Total:</label></td>
 
				<td><input type="text" name="edit13" value="" size="30"  maxlength="24" ></td>
				<td class='label'><label for='blank'></label></td>
			</tr><!--//end total-->
			<tr><!--//begin quota/percentage-->
				<td class='label'><label for='quota'>Quota:</label></td>
 
				<td><input type="text" name="edit20" value="443" size="30"  maxlength="24" ></td>
 
				<td class='label'><label for='percentage'>Percentage:</label></td>
				<td><input type="text" id="percent" name="edit14" onblur="pcent(this.id)" value="22.57%" size="30"  maxlength="24" ></td>			</tr><!--//end quota/percentage-->
		</table>
 
		<table width='100%' border='0' cellpadding='0' cellspacing='0'>
			
			<tr>
				<td align='center'><input type="submit" name="edit" value="Submit" /></td>
			</tr>
 
			
		</table>
 
<br />
 
</form>
 
</body>
</html>

Open in new window

0
Comment
Question by:fcruz5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
gops1 earned 500 total points
ID: 20413646
Add this

document.myform.edit14.value = (total / quota * 100).toFixed(2);
0
 

Author Comment

by:fcruz5
ID: 20413717
Thanks! How would I automatically add the percent sign at the end for that field?
0
 
LVL 17

Assisted Solution

by:gops1
gops1 earned 500 total points
ID: 20413729
document.myform.edit14.value = (total / quota * 100).toFixed(2) + '%';
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question