Javascript - Form result to 2 Decimal Points

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ianhen
    New Member
    • Dec 2006
    • 1

    Javascript - Form result to 2 Decimal Points

    Hi
    I have a Web Form which I need to get results shown to 2 decimal points only.

    In the Head I have: -
    function heatoutput()
    {
    var vWidth=document .heatcalculator .roomwidth.valu e;
    var vLength=documen t.heatcalculato r.roomlength.va lue;
    var vHeight=documen t.heatcalculato r.roomheight.va lue;

    var vRoomSize=vWidt h*vLength*vHeig ht;
    var rlength = 2

    if(document.hea tcalculator.cal cunits.selected Index == 0)
    {vRoomSize=vRoo mSize*0.028368; }

    document.heatca lculator.result .value=((vRoomS ize / 14));
    }

    And the Form is: -
    <FORM name="heatcalcu lator" method="post">
    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#CC000 0">
    <tbody>
    <tr>
    <td><table cellspacing="0" cellpadding="10 " width="100%" border="0">
    <tbody>
    <tr>
    <td><h1 class="Price_Sp ecial">Heat output calculator</h1>
    <p class="Price_Sp ecial">Simply key in your room measurements, choose the correct units and click the "equals" button to find the heat output required to adequately heat your room.</p></td>
    </tr>
    <tr>
    <td><div align="center"> <strong class="Price_Sp ecial">Room measurements in:</strong>
    <select name="calcunits ">
    <option value="feet">Fe et</option>
    <option value="metres" selected>Metres </option>
    </select>
    </div></td>
    </tr>
    <tr valign="center" >
    <td><div align="center" class="Price_Sp ecial"><strong> Length:
    <input size="2" name="roomlengt h">
    &nbsp;&nbsp;&nb sp;&nbsp;Width:
    <input size="2" name="roomwidth ">
    &nbsp;&nbsp;&nb sp;&nbsp;Height :
    <input size="2" name="roomheigh t">
    </strong></div></td>
    </tr>
    <tr valign="center" >
    <td><div align="center" class="Price_Sp ecial"><strong> <a href="JAVASCRIP T:heatoutput()" ><img src="Images/equals.gif" alt="Click for result!" width="24" height="24" border="0" align="absBotto m" class="Price_Sp ecial"></a> &nbsp;
    <input size="10" name="result">
    &nbsp;kW of heat output needed.</strong></div></td>
    </tr>
    </tbody>
    </table></td>
    </tr>
    </tbody>
    </table>
    </FORM>

    How do I change this to show answer to 2 decimal points, please?

    Many thanks.
    Ianhen
  • jai80
    New Member
    • Nov 2006
    • 30

    #2
    Originally posted by ianhen
    Hi
    I have a Web Form which I need to get results shown to 2 decimal points only.

    In the Head I have: -
    function heatoutput()
    {
    var vWidth=document .heatcalculator .roomwidth.valu e;
    var vLength=documen t.heatcalculato r.roomlength.va lue;
    var vHeight=documen t.heatcalculato r.roomheight.va lue;

    var vRoomSize=vWidt h*vLength*vHeig ht;
    var rlength = 2

    if(document.hea tcalculator.cal cunits.selected Index == 0)
    {vRoomSize=vRoo mSize*0.028368; }

    document.heatca lculator.result .value=((vRoomS ize / 14));
    }

    And the Form is: -
    <FORM name="heatcalcu lator" method="post">
    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#CC000 0">
    <tbody>
    <tr>
    <td><table cellspacing="0" cellpadding="10 " width="100%" border="0">
    <tbody>
    <tr>
    <td><h1 class="Price_Sp ecial">Heat output calculator</h1>
    <p class="Price_Sp ecial">Simply key in your room measurements, choose the correct units and click the "equals" button to find the heat output required to adequately heat your room.</p></td>
    </tr>
    <tr>
    <td><div align="center"> <strong class="Price_Sp ecial">Room measurements in:</strong>
    <select name="calcunits ">
    <option value="feet">Fe et</option>
    <option value="metres" selected>Metres </option>
    </select>
    </div></td>
    </tr>
    <tr valign="center" >
    <td><div align="center" class="Price_Sp ecial"><strong> Length:
    <input size="2" name="roomlengt h">
    &nbsp;&nbsp;&nb sp;&nbsp;Width:
    <input size="2" name="roomwidth ">
    &nbsp;&nbsp;&nb sp;&nbsp;Height :
    <input size="2" name="roomheigh t">
    </strong></div></td>
    </tr>
    <tr valign="center" >
    <td><div align="center" class="Price_Sp ecial"><strong> <a href="JAVASCRIP T:heatoutput()" ><img src="Images/equals.gif" alt="Click for result!" width="24" height="24" border="0" align="absBotto m" class="Price_Sp ecial"></a> &nbsp;
    <input size="10" name="result">
    &nbsp;kW of heat output needed.</strong></div></td>
    </tr>
    </tbody>
    </table></td>
    </tr>
    </tbody>
    </table>
    </FORM>

    How do I change this to show answer to 2 decimal points, please?

    Many thanks.
    Ianhen
    hi there,

    try this:

    function heatoutput()
    {
    var vWidth=document .heatcalculator .roomwidth.valu e;
    var vLength=documen t.heatcalculato r.roomlength.va lue;
    var vHeight=documen t.heatcalculato r.roomheight.va lue;

    var vRoomSize=vWidt h*vLength*vHeig ht;
    var rlength = 2

    if(document.hea tcalculator.cal cunits.selected Index == 0)
    {vRoomSize=vRoo mSize*0.028368; }

    var res;
    res=((vRoomSize/14));
    document.heatca lculator.result .value=res.toFi xed(2);

    }

    hope this solves ur problem. Gud Luck!

    cheers,
    jai

    Comment

    Working...