Code

 
  VBScript Routines  
  Decimal to Binary
  Binary to Decimal
  Hex. to Decimal   Decimal to Hex.
 
JavaScript Routines
  setTimeout
  Rollovers
 
HTML
  Web Safe Colors
  Web Safe Color Picker

JavaScript: Format Currency

VBScript has a useful function called FormatCurrency. JavaScript has no direct equivalent, so you have to make your own.

FormatCurrency takes a numeric value and formats it nice and pretty with a leading dollar sign, commas every third digit, and ensures 2 decimal places. For instance, you give it ...

    19534.5

... and it returns ...

    $19,534.50

Here is a JavaScript function that does essentially what the VBScript function of the same name does:

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

 

Tell me what you think