math problem

# Thread: math problem

1. Junior Member
Join Date
Dec 1969
Posts
16

## math problem

I&#039;m having a math problem with my page. This is coming from a previous post on how to add column values. I have an ammount column and a debit/credit column. When I enter an ammount and tab out the ammount column is calculated and the result put in a txt box at the bottom of the page. It also adds up all the ammounts with a debit and all the ammounts with a credit and then subtracts debit from credit.<BR>Now when I try to format what is subtracted, this is where the problem occurs. I want to be able to enter... 100000 or 100 or 100.00 and have it format to dollars and add comas. I think this is what is confusing the math somehow? Here is my code...<BR><BR>In the ammount field--- onBlur="Balance(this)"<BR><BR>function Balance(objName) {<BR> <BR> var matches = /\$?d{1,3}(,d{3})*.d{2}&#124(d*)/;<BR> <BR> var gotIt = matches.exec(objName.value);<BR><BR> if (objName.value != "") {<BR> <BR> if(! gotIt){<BR> alert("Incorrect Currency format"); <BR> objName.select();<BR> objName.focus(); <BR> <BR> } else {<BR> <BR>var total1 = 0; <BR>var total2 = 0; <BR><BR>for (i=0;i&#060;=5i++) <BR>{ <BR> <BR>var odebitCredit = document.posting.elements["debitCredit" + i]<BR>var sDCValue = odebitCredit.optionsodebitCredit.selectedIndex].value;<BR>var ammount = document.posting.elements["ammount" + i]<BR>var vAmmount = document.posting.elements["ammount" + i].value<BR> <BR>if (parseFloat(sDCValue)== 1) <BR>{ <BR>total1+=parseFloat(ammount.value)<BR> <BR>} <BR>else if (parseFloat(sDCValue)== 2) <BR>{ <BR>total2+=parseFloat(ammount.value)<BR>} <BR>} <BR> <BR> var total= parseFloat(total2-total1)<BR> <BR>var conTotal = conCurrency(total);<BR>var conObjName = conCurrency(objName.value);<BR> <BR> <BR>function conCurrency(num) {<BR>num = num.toString().replace(/\$&#124,/g,&#039;&#039;);<BR>if(isNaN(num))<BR>num = "0";<BR>sign = (num == (num = Math.abs(num)));<BR>num = Math.floor(num*100+0.50000000001);<BR>cents = num%100;<BR>num = Math.floor(num/100).toString();<BR>if(cents&#060;10)<BR>cents = "0" + cents;<BR>for (var i = 0; i &#060; Math.floor((num.length-(1+i))/3); i++)<BR>num = num.substring(0,num.length-(4*i+3))+&#039;,&#039;+<BR>num.substring(num.lengt h-(4*i+3));<BR>return (((sign)?&#039;&#039;:&#039;-&#039;) + num + &#039;.&#039; + cents);<BR>}<BR>document.posting.elements["txtTotal"].value = conTotal<BR>objName.value = conObjName<BR> <BR>}<BR>}<BR><BR>}<BR><BR>Sometimes the number is correct in the txtTotal field, then other times it&#039;s grossly incorrect? Any ideas anyone???<BR><BR>Thanks!

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

## I don't understand...

...the point of your regular expression at the start of all that.<BR><BR>You are (a) insisting that the person use commas for entering amounts greater than 999, (b) insisting that the user enter a decimal point, and (b) allowing either 2 digits *or* any number of digits after the decimal point.<BR><BR>This is in direct opposition to what your request says.<BR><BR>But regarding your request: How is the system supposed to know whether<BR> 10000<BR>is supposed to be translated as<BR> 10,000.00<BR>or as<BR> 100.00<BR>???<BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
16

## Ahhhh...

well I don&#039;t know much about regular expressions. I was trying to say that the user has to either enter two decimal places or none like 100.00 or 100 - I didn&#039;t mean to say that they could enter more than 2. I thought that (d*) means they can do only 100 for example if they want.<BR><BR>Regarding the other problem. I think I have fixed it by stripping out the commas before I add with...<BR><BR>floatAmt = ammount.value.replace(/,/g,"");<BR><BR>

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