# Thread: [RESOLVED] Help with Java Calculation

1. Registered User
Join Date
Nov 2009
Posts
13

## [RESOLVED] Help with Java Calculation

Hi everyone, I am new to Javascript and took a class or 2 in college, but need soem help.

I need to make a calculation for a webpage that will do the following:

Client will insert AWG (wire ) Size, then it will derive the correct Circular Mils that the AWG is equal to from an Access Database ( I assume this is the way to set that up or if I can in Javascript itself, thats fine as well ), then Multiply it by the Number of Strands that the Client will also insert.

I had this all set up in Visual Basic and working great, and my boss said he does not want it in a pop up box but prefers it on the website itself hence me reverting to Javascript for this.

To visualize it Image 2 boxes with AWG and STRANDS that the client will put in, then another box with TOTAL CMA where the answer will pop up after it has multipled by the AWG individual CMA and number of Strands.

I have the general idea to set it up, just do not know how to get the Database in there and Code it properly or whatever.

I would appreciate any assistance I can get :-) Thanks everyone.

2. Registered User
Join Date
Nov 2009
Posts
13

I should Add that the AWG will be selected from a list. Here is the page to look at the HTML set up. www.hmwire.com/calculations.html

Thanks

3. Registered User
Join Date
Dec 2008
Posts
488
Any particular reason you need a database to do the calculation?

4. Registered User
Join Date
Dec 2008
Posts
488
A function for returning the CMIL derived from AWG would be: (as you can see, I made it so you can designate "0000" or "4/0" in your option values. I used both in the example)

Code:
```<script type="text/javascript">
function toCMIL(AWG)
{

gauge=AWG.toString();
zeros=/0+/;
numOverZero=/[1-9]\/0/;
nums=/[1-9][0-9]?/;

if(gauge==gauge.match(zeros)) n=1-gauge.length;
else if(gauge==gauge.match(numOverZero)) n=1-parseInt(gauge.charAt(0));
else if(gauge==gauge.match(nums)) n=parseInt(gauge);
else n=NaN;

//If you need more decimal places:
//You can remove Math.round() from the equation,
//then use "return CMIL.toFixed(n);" in place of "return CMIL;"
//where "n"=the number of decimal places.
CMIL = Math.round(Math.pow((5*Math.pow(92,((36-n)/39))),2));
return CMIL;
}
</script>
Please select a wire size: <select onchange="document.getElementById('CMA').value=toCMIL(this.value)">
<option selected="true"></option>
<option value="0000">0000</option>
<option value="3/0">000</option>
<option value="2/0">00</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
</select><br />
Total CMA: <input type="text" id="CMA" /> cmil```
Number of strands does not need to be taken into account to find the CMIL area. At least not if you're finding it according to NEC specs. AWG is the area including strands, so the CMIL will always be the same for a given AWG. The actual overall diameter is what will vary, so stranded wire will be slightly larger in diameter than solid wire, but will have the same AWG/cmil values.
Last edited by jamesbcox1980; 11-09-2009 at 02:22 PM. Reason: Just saw that you didn't want the popup box.

5. Registered User
Join Date
Nov 2009
Posts
13

## Hi James

Hi James,
You are assuming I am trying to figure out the CMil for a single strand of wire, this calculation will be figuring out the Total CMA for multi-strand cable, which is why you need the strands, because the CMil of 1 strand of wire x the strands give you the answer. For example 44 AWG = 4, so IF I have 50 strands of AWG 44 = the Total CMA is gonna be 50 x 4 = 200. This is why i thought I need a database, becaus ein the database, I already set AWG 44 to = 4, so when the client selects 44, it goes to the database and finds 44 = 4 then multiplies by whatever the client places in for strands to get the answer. Does that help?

6. Registered User
Join Date
Dec 2008
Posts
488
Ah, ok, so it just multiplies it? That's easy enough to add in there. So does that answer the question that we don't need the database? Also, what kind of precision do we need to use? The only thing we should definitely need a database for is prices, unless you just use a set price by weight, which you can change easily.
Last edited by jamesbcox1980; 11-10-2009 at 09:26 AM. Reason: critical spelling error :eek:

7. Registered User
Join Date
Nov 2009
Posts
13

## Deicmals is used

Decimals is imperative, it uses decimal to the 1000th. We do not need a database, and I've been told I can set up an array to make the AWG = CMil so I plan to set that up. Is this correct? LOL:-)

8. Registered User
Join Date
Dec 2008
Posts
488
See that's the point. I'm plugged the formula into the javascript function, so you don't need an array or database or anything else to find AWG=CMIL. It's already done in the line:

CMIL = Math.round(Math.pow((5*Math.pow(92,((36-n)/39))),2))

that's based off the formula for converting AWG to CMIL, as specified by the NEC. I'm writing another version with a field for number of strands.

9. Registered User
Join Date
Dec 2008
Posts
488

## HTML for Wire Size Calculator with formula

Ok, here's the new version . I'll post it as code and I'll attach it as an HTML file in a zip folder. This should be everything you need.

Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wire Area Calculator</title>
<style type="text/css">
body
{
margin: 20px;
font-family: "Trebuchet MS", Arial, sans-serif;
font-size: 10pt;
}
#wire_calculator table
{
border: 1px solid #ccc;
background: #FFFFEA;
}
{
background: #eee;
border-bottom: 1px solid #ccc;
}
#wire_calculator select,input[type=text]
{
width: 150px;
}
#wire_calculator input[type="button"]
{
font-weight: bold;
}
</style>
<script type="text/javascript">
<!--//
/* <![CDATA[ */
function toCMIL()
{
\$\$AWG=document.getElementById('AWG').value;
if(\$\$AWG=='') alert('You must choose a wire size.');
\$\$strands=parseFloat(document.getElementById('strands').value);
\$\$gauge=\$\$AWG.toString();

//Regular expressons to see what format was used
\$\$zeros=/0+/;
\$\$numOverZero=/[1-9]\/0/;
\$\$nums=/[1-9][0-9]?/;

if(\$\$gauge==\$\$gauge.match(\$\$zeros)) \$\$n=1-\$\$gauge.length;
else if(\$\$gauge==\$\$gauge.match(\$\$numOverZero)) \$\$n=1-parseInt(\$\$gauge.charAt(0));
else if(\$\$gauge==\$\$gauge.match(\$\$nums)) \$\$n=parseInt(\$\$gauge);
else \$\$n=NaN;

\$\$CMIL = Math.pow((5*Math.pow(92,((36-\$\$n)/39))),2);
\$\$totCmil = \$\$CMIL*\$\$strands;
return \$\$totCmil.toFixed(3);
}
/* ]]> */
//-->
</script>

<body>
<div id="wire_calculator">
<table cellspacing="0" cellpadding="10" border="0">
<tr>
<th colspan="3">Total Wire Area Calculator</th>
</tr>
<tbody>
<tr>
<td align="right"><label for="AWG">Please select a wire size (AWG):</label></td>
<td>
<select name="AWG" id="AWG">
<option selected="selected"></option>
<option value="4/0">4/0</option>
<option value="3/0">3/0</option>
<option value="2/0">2/0</option>
<option value="1/0">1/0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
</select>
</td>
<td></td>
</tr>
<tr>
<td align="right"><label for="strands">Number of Strands:</label></td>
<td>
<select name="strands" id="strands">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected="selected">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select>
</td>
<td></td>
</tr>
<tr>
<th align="right"><label for="CMA">Total CMA (cmil):</label></th>
<td><input type="text" id="CMA" /></td>
<td><input type="button" value="Calculate" onclick="document.getElementById('CMA').value=toCMIL()" /></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>```
Last edited by jamesbcox1980; 11-10-2009 at 10:50 AM. Reason: Forgot to add CDATA tags for XHTML validation

10. Registered User
Join Date
Nov 2009
Posts
13
Wow that is great, the only thing is I would use AWG 10 - 52, so do I just remove the options above 10 and add new options for 40 - 52? The field for strands is not to be a list box, but a text box and the client will put in thier own number, could be up to 10000 Strands, so if you could just make it that way, and report with only that section modified, I think that would be it I owe you big time and appreciate this.

11. Registered User
Join Date
Dec 2008
Posts
488
Cool, no problem, I'll adjust and get it back to you. I need to make an adjustment on my regular expressions anyway. Does AWG 52 exist?

12. Registered User
Join Date
Nov 2009
Posts
13
Haha, Yes it does :-) but going up to 50 is just fine. Here is a good place to get the numbers http://www.interfacebus.com/Copper_Wire_AWG_SIze.html

13. Registered User
Join Date
Dec 2008
Posts
488

## Final revisement

Hmm. I didn't know it went that small. Nevertheless, my function doesn't care how small you go. It will go as small or large as you want.

Anyway, here's the final version--tested, validated, and clean. I commented it a little to kind of give you some direction on making adjustments. Included as CODE below and as a ZIP attachment.

I miss doing electrical work!

HTML Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wire Area Calculator</title>

<!-- Just included some styling, but you can style it however you want -->
<style type="text/css">
body
{
margin: 20px;
font-family: "Trebuchet MS", Arial, sans-serif;
font-size: 10pt;
}
#wire_calculator table
{
border: 1px solid #ccc;
background: #FFFFEA;
}
{
background: #eee;
border-bottom: 1px solid #ccc;
}
#wire_calculator select,input[type=text]
{
width: 150px;
}
#wire_calculator input[type="button"]
{
font-weight: bold;
}
</style>
<script type="text/javascript">
<!--//
/* <![CDATA[ */

/* If your DOCTYPE declaration is not set to XHTML 1.0, remove these CDATA tags */

function toCMIL(AWG,NoOfStrands,precision)
{
\$\$gauge=AWG.toString(); //ensures value is string and not number
\$\$strands=parseFloat(NoOfStrands);
if(\$\$gauge=='') alert('You must choose a wire size.');

//Regular expressons to see what format was used
\$\$zeros=/^0+\$/;
\$\$numOverZero=/^[1-9][\/\-]0\$/;
\$\$nums=/^(([1-9][0-9])|([1-9]))\$/;

if(\$\$zeros.test(\$\$gauge)) \$\$n=1-\$\$gauge.length;
else if(\$\$numOverZero.test(\$\$gauge)) \$\$n=1-parseInt(\$\$gauge.charAt(0),10);
else if(\$\$nums.test(\$\$gauge)) \$\$n=parseInt(\$\$gauge,10);
else \$\$n=NaN;

\$\$CMIL = Math.pow((5*Math.pow(92,((36-\$\$n)/39))),2);
\$\$totCmil = \$\$CMIL*\$\$strands;

return \$\$totCmil.toFixed(precision);
}

/*a seperate function to keep the calculation formula
from becoming proprietary (meaning, so that you can
use it in other applications) */

function calcAwgToCmil()
{
\$\$size=document.getElementById('AWG').value;
\$\$numOfStrands=document.getElementById('strands').value;
\$\$totalField=document.getElementById('CMA');
//the number of decimal places of precision to use in the output
\$\$precision=3;

\$\$totalField.value=toCMIL(\$\$size,\$\$numOfStrands,\$\$precision);
}
/* ]]> */
//-->
</script>

<body>
<div id="wire_calculator">
<table cellspacing="0" cellpadding="10" border="0">
<tr>
<th colspan="3">Total Wire Area Calculator</th>
</tr>
<tbody>
<tr>
<td align="right"><label for="AWG">Please select a wire size (AWG):</label></td>
<td>
<select name="AWG" id="AWG">
<option selected="selected"></option>
<!-- Remember, you can also set the values for "Aught" sizes: value="4/0" or value="0000" -->
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
</select>
</td>
<td></td>
</tr>
<tr>
<td align="right"><label for="strands">Number of Strands:</label></td>
<td>
<!-- this field validates that the user is only inputting numbers -->
<input type="text" name="strands" id="strands" onkeyup="this.value=this.value.match(/\d*/)" />
</td>
<td></td>
</tr>
<tr>
<th align="right"><label for="CMA">Total CMA (cmil):</label></th>
<td><input type="text" id="CMA" /></td>
<td><input type="button" value="Calculate" onclick="calcAwgToCmil()" /></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>```

14. Registered User
Join Date
Nov 2009
Posts
13

## Thanks so much but look

At this http://www.hmwire.com/calculations.html

It is not working, so could you veiw source and see if I'm missing anything?

15. Registered User
Join Date
Nov 2009
Posts
13
Never mindIgot it,Thanks a million :-)