Sorry but.... Im stupid.

1. Member
Join Date
Dec 1969
Posts
94

Sorry but.... Im stupid.

Bill helped me with this a bit last week but I could never get it to work correctly. Using this example, how can I get the number to round 4 places to the right of the decimal. (divide 2 by 3)<BR><BR>&#060;html&#062;&#060;head&#062;<BR>&#06 0;%@ LANGUAGE="VBSCRIPT" %&#062;<BR><BR>&#060;script language="JavaScript"&#062;&#060;/script&#062;<BR><BR>&#060;script&#062; <BR>function total () <BR> {<BR> var Divide = document.form.side[0];<BR> var Multiply = document.form.side[1];<BR> pc = document.form.paycheck.value;<BR> mi = document.form.misc.value;<BR> if (Divide.checked == true)<BR> {<BR> ti = (pc*1)/(mi*1);<BR> document.form.totalinc.value = ti;<BR> }<BR> else if (Multiply.checked == true)<BR> {<BR> ti = (pc*1)*(mi*1);<BR> document.form.totalinc.value = ti;<BR> }<BR> else<BR> {<BR> alert("Please select Divide or Multiply");<BR> }<BR> }<BR><BR>function round(number,X)<BR> {<BR> X = (!X ? 4 : X);<BR> return Math.round(number*Math.pow(10,X))/Math.pow(10,X);<BR> }<BR><BR>&#060;/script&#062;<BR><BR>&#060;TITLE&#062;TEST&#060;/TITLE&#062;<BR>&#060;/head&#062;<BR><BR>&#060;BODY&#062;<BR>&#060;form name="form"&#062;<BR><BR>&#060;INPUT type="radio" value="Divide" name=side onclick="total ();"&#062;Divide &#060;/INPUT&#062;&nbsp<BR>&#060;INPUT type="radio" value="Multiply" name=side onclick="total ();"&#062;Multiply&#060;/INPUT&#062;<BR><BR>&#060;p&#062;<BR><BR><BR>Field One:<BR>&#060;INPUT type="text" name="paycheck" size="25" maxlength="25" value="" onchange="total ()"&#062;&#060;/INPUT&#062;<BR> <BR><BR>Field Two:<BR>&#060;INPUT type="text" name="misc" size="25" maxlength="25" value="" onchange="total ()"&#062;&#060;/INPUT&#062;<BR><BR><BR>&#060;HR&#062;<BR>TOTAL:&#0 60;/font&#062;&#060;/td&#062;<BR>&#060;td&#062;&#060;input Name="totalinc" type="text" maxlength="25" size="25" value="" onchange="round()" readonly&#062;&#060;/td&#062;<BR><BR>&#060;/tr&#062;<BR>&#060;/form&#062;<BR>&#060;/body&#062;<BR>&#060;html&#062;

2. Senior Member
Join Date
Dec 1969
Posts
96,118

Almost there...

You do:<BR><BR>&#060;script&#062; <BR>function total () <BR>{ <BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; document.form.totalinc.value = ti; <BR>&nbsp; &nbsp; ...<BR>} <BR><BR>but that code NEVER CALLS your "round" function!<BR><BR>You need to do:<BR><BR>function total () <BR>{ <BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; document.form.totalinc.value = round(ti,4); <BR>&nbsp; &nbsp; ...<BR>} <BR><BR>** ALSO **<BR><BR>You did NOT use the round code I showed you. The code you are using *might* work, but:<BR><BR>(a) Math.pow(10,X) does *NOT* necessarily return a perfect result! For example, Math.pow(10,4) *might* give you 9999.999987 instead of 10000.0! For safety, you should either multiply integers the requisite number of times *or* round the Math.pow result, such as: Math.round(Math.pow(10,X)+0.5)<BR><BR>(b) When you then turn around and divide by the number you multiplied by, you are reintroducing the possibility of *NOT* getting exactly 4 digits! Consider: Suppose the number is 3/4. Or 0.75. Now you multiply that by 10000 and get 7500 and then you divide by 10000...and get 0.75!!! So it displays as 0.75 with only *two* digits right of the decimal point! Can you say "ugh!" Also, it is quite possible that, when you divide by 10000, you end up with a number that JS *still* won&#039;t display with *only* 4 digits to the right of the decimal point! Floating point math is tricky, and *IT IS IMPOSSIBLE* to exactly generate *most* decimal fractions with the binary numbers used by computers. Even 1/10th, for example, has to be represented as an infinitely repeating sequence of binary digits (the same way that 1/3 in decimal comes out as 0.33333333.... forever!). So 99% of all decimal numbers are just approximations if you don&#039;t have an infinitely large computer.<BR><BR>That is why I suggested using *STRING* manipulation to format the numbers, instead. And I would also, for simplicity&#039;s sake, write a different round routine for each number of digits right of the decimal point. (Surely, in a given page, you&#039;d need at most two or three of them?)<BR><BR>SO...<BR><BR>// round to 4 digits<BR>function round4( num ) <BR>{ <BR>&nbsp; &nbsp; var snum = "" + Math.round( num * 10000 )<BR>&nbsp; &nbsp; var slen = snum.length;<BR>&nbsp; &nbsp; switch( slen )<BR>&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; case 0: // impossible???<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0.0000";<BR>&nbsp; &nbsp; &nbsp; &nbsp; case 1: <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0.000" + snum;<BR>&nbsp; &nbsp; &nbsp; &nbsp; case 2:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0.00" + snum;<BR>&nbsp; &nbsp; &nbsp; &nbsp; case 3:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0.0" + snum;<BR>&nbsp; &nbsp; &nbsp; &nbsp; case 4: <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "0." + snum;<BR>&nbsp; &nbsp; &nbsp; &nbsp; default:<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return snum.substring(0,slen-4) + "." + snum.substring(slen-4);<BR>&nbsp; &nbsp; }<BR>}<BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•