Sign in to follow this  
Arkantis

[web] Javascript WTH?

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this