...

View Full Version : Newbee Needs Help with formatting Commas in numbers



kadet
07-12-2007, 06:26 PM
I have a script written, but it won't format numbers like I need it to. Here is the script, what am I doing wrong? Please be specific and simple as I am very new to JS. Thanks in advance.

<!-- CALCULATOR -->

<html>
<head>
<title>Calculator</title>

<script language="JavaScript">

function CommaFormatted(amount)
{
var delimiter = ",";
var a = amount.split('.',2)
var d = j[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
j.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}



function CalculateSum(Atext, Btext, Ctext, Dtext, Etext, Ftext, Gtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext)/100;
var E = parseFloat(Etext);
var F = parseFloat(Ftext);
var G = parseFloat(Gtext);
var H = parseFloat(E+F)/100;
var result = parseFloat ((A * B * 30)*(1-D)*(1-H)*(G/100)-C);

form.result.value= result;
}


function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.input_D.value = "";
form.input_E.value = "";
form.input_F.value = "";
form.input_G.value = "";
form.result.value = "";
}

</script>

</head>
<body text="white" bgcolor="#990000" background="">

<p><div align="left"><strong><font color="#ffff00" size="2">
<TABLE borderColor=#ff0000 width="100%" border=6>

<TR><!-- Row 1 -->
<TD>
<P align=center><STRONG><FONT color=#ffff00 size=2>Net
Profit&nbsp;Calculator</FONT> <font color="#ffff00" size="2">For Oil &amp; Gas
Wells</font></STRONG></P></TD><!-- Col 1 --></TR></TABLE>
</font></strong></div><font size="5">
<center>
<table height="320" width="956" align="left" border="0">
<TBODY>

<tr><!-- Row 1 -->
<td>
<DIV align=center>

<form name="Calculator" method="post">
<P align=left>&nbsp;&nbsp;&nbsp;&nbsp;<BR>
<TABLE width="100%" border=0>

<TR><!-- Row 1 -->
<TD width=443>
<P align=right>Total Daily Production&nbsp;</P></TD><!-- Col 1 -->
<TD>
<input style="WIDTH: 98px; HEIGHT: 25px" size="4"
name="input_A">&nbsp;<FONT size=1>(All Wells)&nbsp;</FONT></TD><!-- Col 2 --></TR>
<TR><!-- Row 2 -->
<TD width=443>
<P align=right>Price
Per&nbsp;Unit <FONT size=2>$</FONT>&nbsp;</P></TD><!-- Col 1 -->
<TD>


<input style="WIDTH: 96px; HEIGHT: 25px" size="4" name="input_B"
>&nbsp;<FONT size=1>(Barrel / MCF)</FONT></TD><!-- Col 2 --></TR>
<TR><!-- Row 3 -->
<TD width=443>
<P align=right>

Field Expenses&nbsp;<font size="4"><font size="2">$</font></font></P></TD><!-- Col 1 -->
<TD><input style="WIDTH: 94px; HEIGHT: 25px" size="3" name="input_C"
>&nbsp;<FONT size=1>(Monthly Supervision and
Maintainence)</FONT></TD><!-- Col 2 --></TR>
<TR><!-- Row 4 -->
<TD width=443>
<P align=right>State Severance Tax&nbsp;&nbsp;%</P></TD><!-- Col 1 -->
<TD>

<input style="HEIGHT: 25px" size="4" name="input_D"> <font size="1">(State
Tax)</font></TD><!-- Col 2 --></TR>
<TR><!-- Row 5 -->
<TD width=443>
<P align=right>LORI&nbsp;&nbsp;%</P></TD><!-- Col 1 -->
<TD>
<input style="HEIGHT: 25px" size="4" name="input_E">&nbsp;<FONT size=1>(Land Owner Royalty
Interest)</FONT>&nbsp;</TD><!-- Col 2 --></TR>
<TR><!-- Row 6 -->
<TD width=443>
<P align=right>ORRI&nbsp;&nbsp;%</P></TD><!-- Col 1 -->
<TD>
<input style="HEIGHT: 25px" size="4" name="input_F">&nbsp;<FONT size=1>(Override Royalty
Interest)</FONT></TD><!-- Col 2 --></TR>
<TR><!-- Row 7 -->
<TD width=443>
<P align=right>NRI&nbsp;&nbsp;%</P></TD><!-- Col 1 -->
<TD> <input style="HEIGHT: 25px" size="4" name="input_G">&nbsp;<FONT size=1>(Net Revenue
Interest&nbsp;of
Investor)</FONT></TD><!-- Col 2 --></TR></TABLE>&nbsp;
<TABLE width="100%" border=0>

<TR><!-- Row 1 -->
<TD>
<P align=center>

<input style="WIDTH: 189px; HEIGHT: 28px" onclick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form.input_D.value, this.form.input_E.value, this.form.input_F.value, this.form.input_G.value, this.form)" type="button" size="34" value="Click Here to Calculate" name="AddButton"></P></TD><!-- Col 1 --></TR></TABLE>&nbsp;<font size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<TABLE height=53 width=950 border=0>

<TR><!-- Row 1 -->
<TD width=446>
<P align=right><STRONG>Net Profit</STRONG>&nbsp; $</P></TD><!-- Col 1 -->
<TD> <input size="7" name="result" style="WIDTH: 93px; HEIGHT: 25px">&nbsp;<FONT size=2>Per Month</FONT><FONT
size=1>&nbsp;
</FONT></TD><!-- Col 2 --></TR></TABLE></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<TABLE width="100%" border=0>

<TR><!-- Row 1 -->
<TD>
<P align=center>

<input style="WIDTH: 108px; HEIGHT: 28px" onclick="ClearForm(this.form)" type="button" size="16" value="Reset Form" name="ClearButton"></P></TD><!-- Col 1 --></TR></TABLE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font size="1">

<br></font><font size="1"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>
</form></DIV><br></font></TD><!-- Col 1 --></TR></TBODY></TABLE></CENTER></FONT>





</body>
</html>

glenngv
07-12-2007, 06:34 PM
http://www.codingforums.com/showthread.php?t=21345

kadet
07-12-2007, 06:45 PM
http://www.codingforums.com/showthread.php?t=21345

Can you be more specific, do I have things in the right place etc..?

glenngv
07-12-2007, 06:51 PM
I just redirected you to working alternative codes. I actually didn't analyze your code.

kadet
07-12-2007, 07:08 PM
Glen,

Thanks for the redirect. I tried to copy & paste the code I saw on the redirect, but it didn't work either. I must be putting something in the wrong place or not calling the function correctly. Would you mind to look at the code and see if you can spot my problem? You seem very knowledgeable. I would really appreciate your help.

glenngv
07-12-2007, 07:19 PM
I took the currency code from one of the scripts in the page I linked you to.

String.prototype.currencyFormat = function() {
isNaN(parseFloat(this)) ? c = 0 : c = parseFloat(this);
v = (Math.round(Number(c) * 100) / 100).toString();
g = v.split(".");
t = g[0].split("");
for (i = t.length - 1; i > -1; i -= 3) {
i != t.length - 1 ? t[i] += "," : null;
}
f = String(Math.round(Math.abs(v) % 1 * 100));
f.length == 1 ? f = ".0" + f : f = "." + f;
return t.join("") + f;
};

function CalculateSum(Atext, Btext, Ctext, Dtext, Etext, Ftext, Gtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext)/100;
var E = parseFloat(Etext);
var F = parseFloat(Ftext);
var G = parseFloat(Gtext);
var H = parseFloat(E+F)/100;
var result = parseFloat ((A * B * 30)*(1-D)*(1-H)*(G/100)-C);

form.result.value= (result+"").currencyFormat();
}

kadet
07-12-2007, 07:56 PM
Thank You Glenn!!!! You rock!!! Works perfectly!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum