



/* Argument x:      real, the number to be formatted.
   Argument places: int, the number of decinal places.
   Result:          string.
   This function converts the number to a string,
   formatting so it has 'places' decimal places.
   It inserts commas at every third digit before the
   point.
*/
function formatReal( x, places )
{
  return formatCurrency( x, places, '' );
}


/* Argument x:      real, the number to be formatted.
   Argument places: int, the number of decinal places.
   Argument prefix: string, a currency prefix.
   Result:          string.
   This function converts the number to a string,
   formatting so it has 'places' decimal places.
   The currency prefix will be placed before the 
   number and any sign, as normal for UK money.
   It inserts commas at every third digit before the
   point.
*/
function formatCurrency( x, places, prefix )
{
	var pos;
	var nNum = x;
	var nStr;    
	var splitString = new Array(2);   

	nNum = formatterRound( nNum, places );

	nStr = formatterPreserveZeros( Math.abs(nNum), places ); 
        // Make nNum into a string.

	if (nStr.indexOf('.') == -1) {
	  splitString[0] = nStr;
	  splitString[1] = '';
	}
	else
	  splitString = nStr.split('.', 2);

        /* Insert commas.
        */
	pos = splitString[0].length;
	while (pos > 0) {
	  pos -= 3;
	  if (pos <= 0) 
            break;

	  splitString[0] = splitString[0].substring(0,pos)
			 + ',' 
		    	 + splitString[0].substring(pos, splitString[0].length);
	}
	
	/* Insert decimal point.
        */
	if (splitString[1].length > 0) 
	  nStr = splitString[0] + '.' + splitString[1];
	else
	  nStr = splitString[0];
	
	/* Do minus sign and currency prefix.
        */
        var sign = '';
		
	if (nNum < 0)
          sign = '-';
      
	nStr = prefix + sign + nStr;
	
	return (nStr);
}


/* Argument x:      real, the number to be formatted.
   Argument places: int, the number of decinal places.
   Result:          real.
   Rounds the number to the specified number of places.
 */
function formatterRound( x, places )
{
	var factor;
	var i;

	/* Round to a certain precision.
        */
	factor = 1;
	for (i=0; i<places; i++)
	  factor *= 10;

	x *= factor;
	x = Math.round(x);
	x /= factor;

	return (x);
}


/* Argument x:      real, the number to be formatted.
   Argument places: int, the number of decinal places.
   Result:          string.
   This function converts the number to a string,
   ensuring that it has the specified number of places
   by padding with zeros.
 */
function formatterPreserveZeros( x, places )
{
	var i;

	/* Make a string to preserve the zeros at the end.
        */
	x = x + '';

	if ( places <= 0 ) 
          return x; 
          // Leave now. no zeros are necessary.
	
	var decimalPos = x.indexOf('.');

	if (decimalPos == -1) {
	  x += '.';
	  for (i=0; i<places; i++) 
	    x += '0';
	}
	else {
          var actualDecimals = (x.length - 1) - decimalPos;
	  var difference = places - actualDecimals;
	  for (i=0; i<difference; i++)
	    x += '0';
	}
	
	return x;
}


