• Advertisement
Sign in to follow this  

[web] Javascript WTH?

This topic is 4619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a web based system I developed for a trucking company and they required a check clearing page where it would list open invoices and you would check processed if they were on the check received... At the same time each radio button was given an on mouse down function. Its done in PHP/MySql/JS heres the problem function
echo "<script>
		var total = [0];
		function EnterCheck(elem, which, field, val)
		{
			var t = field.value - [1] + [1];
			if(which == 'No')
			{
				if(elem.checked == false)
					total -= val;
			}
			else if(which == 'Yes')
			{
				if(elem.checked == false)
					total += val;
			}
			total = Math.round(total*[100])/[100];
			field.value = total;
		}</script>";
And heres the button:
<td><input type='hidden' name='h$id' value='".$t['total2']."'>
	<input type='radio' name='r$id' value=1
	onMouseDown='EnterCheck(this, \"Yes\", document.forms.check.total_processed, ".$t['total2'].")'>Yes
	<input type='radio' name='r$id' value=0 checked 
		onMouseDown='EnterCheck(this, \"No\", document.forms.check.total_processed, ".$t['total2'].")'>No
</td>
The function is supplied with an unformatted number from the SQL query, i used a formatted for display. I know some of the totals in the DB store beyond the 100th decimal point but if I dont add the line
 total = Math.round(total*100)/100;
to the JS function before overwriting the Total field on the top of the page.. To JS in this function without the last line above 1066.63 - 269.54 = 797.090000000001 in JS what am I doing wrong? I'm positive that the numbers being provided do not extend into the 1 millionth decimal point.... Is this an issue with JS?

Share this post


Link to post
Share on other sites
Advertisement
It's an issue with floating point math. You'll experience this no matter what programming language you use. It's just the way things work. I'd advice you to just truncate the result.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement