Solved

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

Posted on 2007-12-05
3
1,888 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now