www.webdeveloper.com
Results 1 to 5 of 5

Thread: Help calculation, decimals and currency symbol

  1. #1
    Join Date
    May 2013
    Posts
    3

    Help calculation, decimals and currency symbol

    My form calculates correctly (syntax could probably be improved), but I've tried several options to show two decimal places on some of the fields with no success. It would be great to have code that would add a comma where necessary, and also to add a $ for currency.

    Code:
    <form method="post" name="calculate">
    						<div class="floatleft">
    							<table style="width: 384px;" border="0" cellspacing="0" cellpadding="12" bgcolor="#FECB00">
    								<tbody>
    									<tr>
    										<td colspan="2" bgcolor="#000000"><img src="images/stories/ownermenu/singlesockchart.png" alt="Single Sock" width="384" height="35" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 5px; height: 5px;" align="left" valign="middle" width="61%">&nbsp;</td>
    										<td style="text-align: center;" align="left" valign="middle" width="39%">&nbsp;</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Pairs of shoes sold annually</td>
    										<td style="text-align: center;" align="left" valign="middle" width="39%"><input id="shoessingle" type="text" name="shoessingle" size="15" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000; font-style: normal; font-weight: bold;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Sock to Shoe Ratio</td>
    										<td style="text-align: center;" align="left" valign="middle"><input name="socktoshoe" type="text" disabled id="socktoshoe" value="1.5" size="15" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Average Sock Selling Price (Pair)</td>
    										<td style="text-align: center;" align="left" valign="middle"><input id="singlepair" type="text" name="singlepair" size="15" onchange="amargin(); update(); gmargin();  formatCurrency(grossmargin.value);" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Average Gross Margin (50%)</td>
    										<td style="text-align: center;" align="left" valign="middle"><input name="avgmarg" type="text" disabled id="avgmarg" size="15" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Total Sock Sales</td>
    										<td style="text-align: center;" align="left" valign="middle"><input name="totalsales" type="text" disabled id="totalsales" size="15" />
    										</td>
    									</tr>
    									<tr>
    										<td style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 26px; height: 26px; color: #000000; font-weight: bold;" align="left" valign="middle" width="61%">&nbsp; &nbsp;Gross Margin</td>
    										<td style="text-align: center;" align="left" valign="middle"><input id="grossmargin" type="text" name="grossmargin" disabled="disabled" size="15" />
    										</td>
    									</tr>
    								</tbody>
    							</table>
    						</div>
    					</form>
    					
    <script type="text/javascript">
    	// <![CDATA[
    	function update() {
    		document.calculate.totalsales.value = parseFloat(document.calculate.shoessingle.value) * parseFloat(document.calculate.socktoshoe.value) * parseFloat(document.calculate.singlepair.value);
    	}
    	// ]]>
    	
    </script>
    <script type="text/javascript">
    	// <![CDATA[
    	function amargin() {
    		document.calculate.avgmarg.value = parseFloat(document.calculate.singlepair.value) /2;
    	}
    	// ]]>
    	
    </script>
    
    
    <script type="text/javascript">
    	// <![CDATA[
    	function gmargin() {
    		document.calculate.grossmargin.value = parseFloat(document.calculate.totalsales.value) /2;
    	}
    	// ]]>
    	
    </script>

  2. #2
    Join Date
    Oct 2010
    Location
    UK
    Posts
    17
    This might be what you're looking for:

    toFixed
    https://developer.mozilla.org/en-US/...Number/toFixed

  3. #3
    Join Date
    May 2013
    Posts
    3

    probably, but failed to work

    I tried using those, but it stops my function when I do, so my implementation of Tofixed must be incorrect. Anyone know how it should be applied?

  4. #4
    Join Date
    Oct 2010
    Location
    UK
    Posts
    17
    Code:
    function gmargin() {
        //calculate the new gross margin
        var newgrossmargin = parseFloat(document.calculate.totalsales.value/2);
    
        //fix this new gross margin value to 2 decimal places in all cases.
        newgrossmargin = newgrossmargin.toFixed(2);
    		
        //inject this new value into the input box
        document.calculate.grossmargin.value = newgrossmargin;
    }

  5. #5
    Join Date
    May 2013
    Posts
    3
    great, Aralus. That works. Now is there a way to add a comma and maybe currency symbol to make it format like $45,000?

    I found this code but not sure how I could implement it...
    Code:
    function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles