calculation problems

1. Senior Member
Join Date
Dec 1969
Posts
2,493

calculation problems

How do I subtract 2 form field values , I have tried parseFloat() and using -0<BR><BR>the values would be like 4,086.44-832.65<BR><BR>I have searched irt.org with no good results<BR><BR>document.formula.NRGrossMargin.valu e = parseFloat(document.formula.NRAVGPrice.value)-parseFloat(document.formula.NRAVGDirect_Cost.value );<BR><BR><BR>document.formula.NRGrossMargin.value = (document.formula.NRAVGPrice.value-0)-(document.formula.NRAVGDirect_Cost.value-0);<BR><BR>

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

That *should* work...

document.formula.NRGrossMargin.value = <BR> parseFloat(document.formula.NRAVGPrice.value)<BR> - parseFloat(document.formula.NRAVGDirect_Cost.value );<BR><BR>Assuming, of course, that "formula" is the name of your form and the names starting with "NR" are text fields in that form.<BR><BR>What happens? Anything?<BR><BR>Are you sure you&#039;re even *getting* to this code?<BR><BR>I hope you don&#039;t really have a comma in that one number; I don&#039;t think parseFloat likes commas.<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
2,493

RE: That *should* work...

when I do <BR><BR>alert(parseInt(document.formula.NRAVGPrice .value) + &#039;, &#039; + parseInt(document.formula.NRAVGDirect_Cost.value) );<BR><BR>from 4,086.44 and 832.65<BR><BR>I get (4, 832)<BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,493

OOPS!

sorry that should be parseFloat I was tring parseInt()<BR><BR>with alert(parseFloat(document.formula.NRAVGPrice.value ) + &#039; &#039; + parseFloat(document.formula.NRAVGDirect_Cost.value ) );<BR><BR>I get 4, 832.65<BR><BR>so when I run this function<BR><BR>function getinfo()<BR>{<BR>alert(parseFloat(document.formul a.NRAVGPrice.value) + &#039; &#039; + parseFloat(document.formula.NRAVGDirect_Cost.value ) );<BR>var GrossMargin = 0<BR>var GrossSales = 0<BR>document.formula.NRGrossMargin.value = parseFloat(document.formula.NRAVGPrice.value)-parseFloat(document.formula.NRAVGDirect_Cost.value );<BR>GrossMargin = GrossMargin + (document.formula.NRAVGDirect_Cost.value-0);<BR>GrossSales = GrossSales + (document.formula.NRAVGPrice.value-0);<BR>document.formula.GrossTotals.value = GrossSales;<BR>document.formula.GrossMarginTotals. value = GrossMargin;<BR>}<BR>it puts -828.65 in the field named NRGrossMargin<BR>

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

BINGO

See, it doesn&#039;t like your comma!<BR><BR>(And why did you use parseInt, instead of parseFloat?)<BR><BR>It chopped that number off, right with the comma!<BR><BR>So you should remove spaces and commas (and dollar signs?) from the .value before doing the parseFloat.<BR><BR>var frm = document.formula; // neater code<BR>var price = frm.NRAVGPrice.value; // get the two source values<BR>var cost = frm.NRAVGDirct_Cost.value;<BR><BR>// we want to remove all whitespace, commas, and dollar signs...<BR>// (the backslashes before comma and dollar sign are just my paranoia)<BR>var re = /[s,\$]/g<BR><BR>// do this for both form field values...<BR>price = parseFloat( price.replace(re,"") );<BR>cost = parseFloat( cost.replace(re,"") );<BR><BR>// put difference in place.<BR>frm.NRGrossMargin.value = ( price - cost );<BR><BR>*******************<BR><BR>You probably want to do pretty formatting on that GrossMargin value. Do you know how to do that?<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
2,493

RE: BINGO

that works :)<BR><BR>I was trying parseInt() out of desperation I figured it wouldn&#039;t work<BR><BR>and I did show the , in my first post<BR><BR>Thanks

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

You must have missed....

...this last line in my first post:<BR><BR>** QUOTE **<BR>I hope you don&#039;t really have a comma in that one number; I don&#039;t think parseFloat likes commas.<BR>** END QUOTE **<BR><BR>See, I *saw* your comma. And commented on it.<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
•