Convert Input Number To Dollar Figure

[Total: 0    Average: 0/5]

This will convert a form input number to a dollar representation.

Body Code:

<form>
<input name=t value="123453405.09">
<input type=button onclick="doIt(this.form)" value="convert now">
</form>
<script>
function doIt(_f)
{
  _f.t.value=convertIt(_f.t.value);
}
function convertIt(_v)
{
  var _dollars=parseInt(_v);
  var _cents=parseInt((_v-_dollars)*100);
  var _negative=_dollars<0;
  if(_negative){_dollars=-_dollars;_cents=-_cents;}
  while(_cents.toString().length<2)_cents="0"+_cents;
  var _dA=_dollars.toString().split("");
  var _d="";
  for(var i=_dA.length-1;i>=0;i--)
  {
    var _comma="";
	if((_dA.length-i)%3==0 && i!=0)_comma=",";
    _d=_comma+_dA[i]+_d;
  }
  var _neg_sign=_negative?"-":"";
  var _result="$"+_neg_sign+_d+"."+_cents;
  return _result;
}
var _A=new Array(0,-10,-1000.32,-123456789.123456,-0,10,1000.32,123456789.123456,123.12,1.1);
for(var i=0;i<_A.length;i++)
  document.write("<HR>Convert "+_A[i]+":<BR><DIR>"+convertIt(_A[i])+"</dir><br>");
</script>

Times Viewed: 9

Leave a Reply

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