Dollar Value Function

[Total: 1    Average: 4/5]

Handy little dollar value function returns rounded number suitable for display on web page.

Head Code:

<script language = "JavaScript">
function round()
{
  var xyz = window.document.xyzFORM.RAW_NUMBER.value;
  var dollarVal = dollarValue(xyz);
  window.document.xyzFORM.ROUNDED_NUMBER.value = "$  " + dollarVal;
}
function dollarValue(n)
{
  var Bstr = "";
  var b = n * 100;
  var c = Math.round(b);
  var Astr = c.toString();
  var Alength = Astr.length;
  if (Alength <= 2) Bstr = Bstr + ".";
  var dot = Alength - 3;
  var counter = 0;
    while (counter < Alength )
    {
      Bstr = Bstr + Astr.charAt(counter);
      if (counter == dot) Bstr = Bstr + ".";
      counter = counter + 1;
    }
  var d = Bstr;
  return d;
}
</script>

Body Code:

<form name = "xyzFORM"  action = "#">
<table align = "center"  width = "65%"cellpadding = "5">
<tr>
<td colspan = "2"><font size = "2">Typically when you multiply quantity times price of an item, JavaScript doesn't round the result to a convenient dollar amount of two decimal places.  The JavaScript in the header of this page provides a handy function where the parameter is returned rounded to two decimal places suitable for dollar value display on your web page.</td>
</tr>
<tr>
<td align = "right">Example raw number: </td>
<td align = "left"><input type = "text" name = "RAW_NUMBER" size = "10" value = "14.9543827"></td>
</tr>
<tr>
<td colspan = "2" align = "right"><b>
<input type = "button" name = "BUTTON" value = "Round to Dollar Value" onClick = "round()"></b>
</tr>
<tr>
<td align = "right">Number rounded to a dollar value.</td>
<td align = "left"><input type = "text" name = "ROUNDED_NUMBER" size = "10"></td>
</tr>
</table>
</form>

Times Viewed: 5

Leave a Reply

Your email address will not be published. Required fields are marked *