//* FILE **********************************************************************
//*                                                                          **
//* NAME:Math.js                                                             **
//*                                                                          **
//* GOAL: functions manipulating numbers                                     **
//*                                                                          **
//*****************************************************************************
//*                                                                          **
//* MODIFICATIONS:                                                           **
//*                                                                          **
//* Version  Date         Author  Comment                                    **
//*                                                                          **
//* 1.0      05 May 2000  MV      Original version                           **
//* 1.01     18 May 2000  MV      Add fTrunc, fFormatAtDecimal, fDecimalPart **
//* 1.02     28 Mar 2001  MV      Debug fFormatAtDecimal                     **
//* 1.03     23 Jul 2001  MV      Add fLeftJustify                           **
//*                                                                          **
//* FILE **********************************************************************

//************
// CONSTANTS *
//************

//*************************
// FUNCTIONS DECLARATIONS *
//*************************


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fTrunc                                                             **
//* GOAL: return integer part of given float                                 **
//*                                                                          **
//***************************************************************** FUNCTION **
function fTrunc(piValue)
{
  if (piValue > 0)
  {
    return Math.floor(piValue);
	}
  else
  {
    return -Math.floor(-piValue);
  }
} // fTrunc


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fRoundAtDecimal                                                    **
//* GOAL:                                                                    **
//*                                                                          **
//***************************************************************** FUNCTION **
function fRoundAtDecimal(piValue, piDecimal)
{    
  if (isNaN(piValue))
  {
    return 0;
  }
  else
  {
    return (Math.round(piValue*Math.pow(10,piDecimal)) / Math.pow(10,piDecimal));
  }
} // fRoundAtDecimal


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fFormatAtDecimal                                                   **
//* GOAL: return string corresponding to input number, at given decimal      **
//*                                                                          **
//***************************************************************** FUNCTION **
function fFormatAtDecimal(piValue, piDecimal)
{
  var i;
  var val, decimalPart, res, addZeros;
  
  if (piDecimal == 0)
  {
    return Math.round(piValue).toString();
  }

  val = fRoundAtDecimal(piValue, piDecimal);
  decimalPart = Math.abs(Math.round(Math.pow(10,piDecimal)*(val - fTrunc(val)))).toString();

  addZeros = piDecimal - decimalPart.length;
  for (i = 0; i < addZeros; i++)
  {
    decimalPart = '0'+decimalPart;
  }

  if (val < 0)
  {
    return '-' + Math.floor(-val) + '.' + decimalPart;
  }
  else
  {
    return Math.floor(val) + '.' + decimalPart;
  }

} // fFormatAtDecimal

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fLeftJustify                                                       **
//* GOAL: Fill number with 0 in front, to format it at given number of digits**
//*       Optional piFiller may be used to fill with another char than '0'   **
//*                                                                          **
//***************************************************************** FUNCTION **
function fLeftJustify(piNumber, piDigits, piFiller)
{
  var num = ''+piNumber;
  var filler = piFiller?piFiller:'0';
  while (num.length < piDigits)
  {
    num = filler+num;
  }

  return num;

} // fLeftJustify

