function Num2String (value,digits,LowerSciExp,UpperSciExp,LowerDecExp,UpperDecExp)
{
    //***********************************************************************
    //*
    //* Function Num2String converts a number to an output string in scientific 
    //*                     or decimal notation with specified numbers of digits,
    //*                     and ranges for each type of return (scientific or decimal)
    //*                     Num2String is intended to preprocess a number before writing
    //*                     to a text box.  JavaScript does not format numbers natively.
    //*
    //* Language = JavaScript1.1
    //*
    //* Inputs:
    //*   value  = number to convert to a string
    //*   digits = number of significant digits desired in output
    //*   LowerSciExp = exponent of the lower limit range for scientific notation
    //*   UpperSciExp = exponent of the upper limit range for scientific notation
    //*   LowerDecExp = exponent of the lower limit range for decimal notation
    //*   UpperDecExp = exponent of the upper limit range for decimal notation
    //*
    //* Returns:
    //*   string = representation of value, formatted according to inputs
    //*
    //* Example:  
    //*     var value = 345010;
    //*     var output = Num2String(value,3,-7,7,-4,4);
    //*     output has the value  "3.45e5"
    //*     var value = 345010;
    //*     var output = Num2String(value,5,-7,7,-4,4);
    //*     output has the value  "3.4501e5"
    //*     var value = 3450.1;
    //*     var output = Num2String(value,3,-7,7,-4,4);
    //*     output has the value  "3450."
    //*
    //*
    //* Specifications:
    //*   More negative than -10^7 ||| (-10^7 to -10^4) ||| (-10^4 to -10^-4) ||| (-10^-4 to -10^-7) ||| (-10^-7 to 0)
    //*        "below range"       |||       sci.       |||        dec        |||        sci         |||    sci 
    //*
    //*    (0 to 10^-7) ||| (10^-7 to 10^-4) ||| (10^-4 to 10^4) ||| (10^4 to 10^7) |||  > 10^7
    //*       sci       |||       sci        |||        dec      |||      sci       ||| "above range"
    //*
    //* The code should distinguish between the 0 to 10^-7 and 10^-7 to 10^-4 ranges, even though the notation
    //*  chosen would be the same.  There may be a need later to use something else for the 0 to 10^-7 range.
    //*
    //* Author of original version:
    //*  Dr. Jim Weaver
    //*  Regulatory Support Branch
    //*  Ecosystems Research Division
    //*  National Exposure Research Laboratory
    //*  United States Environmental Protection Agency
    //*  960 College Station Road
    //*  Athens, Georgia 30605
    //*  weaver.jim@epa.gov
    //*
    //* Author of current version:
    //*  Kevin Weinrich
    //*  OAO Corp.
    //*  Contractor to US EPA
    //* 
    //* Reference:
    //*  Flanagan, D., 1997, JavaScript: The Definitive Guide, 2ed, O'Reilly and Associates
    //*
    //* Created: 10-6-1999
    //*          10-23-1999 Rounding added
    //*          5-26-2000 Corrected rounding, extra "0" digit after decimal,
    //*			insufficient digits in mantissa
    //*          6-2-2000  Original code replaced by following routines, KW
    //*	     6-2-2000  Changed ranges of sci. vs. dec. notation, based on client specification, KW
    //*          6-7-2000  Added variable definitions, KW
    //*          6-28-2000  Removed extra "}" at end of function ScientificNotation which caused error on MSIE, JWW
    //*          8-30-2002  Returns the value if not a number, JWW
    //*
    //* Requires: isNaN, Math., RegularNotation, ScientificNotation
    //*
    //***********************************************************************

    var iMagnitude; // iMagnitude is the lowest power of 10 that the input value is less than
    var strNewVal; // Build string output
    var strTemp;
    var strMantissa; // Mantissa part of scientific notation
    var strExponent; // Exponent part of scientific notation
    var bNegativeNum; // Flag for negative input value
    var dThisLimit; // Some power of 10 , used as a limit to test against
    var dAbsValue; // Absolute value of input value
    var dMagnifier; // dMagnifier is the power of 10 necessary to convert "value" to a number whose units
		    //  are the rightmost significant digit before rounding.
    var dAdjustedVal; // value, after it's converted  to a number whose units
    		    //  are the rightmost significant digit before rounding.
		    // e.g., if value = 0.04516, and digits = 3, dAdjustedVal = 451.6
    var dRoundedVal; // rounded dAdjustedVal - e.g., 452
    var dLowerDecLimit; // Some power of 10, lower limit of decimal notation
    var dUpperDecLimit; // Some power of 10, upper limit of decimal notation
    var dInnerLimit; // Some power of 10, narrow limit around 0.0 for inner range of scientific notation

    //return the value for non numbers (8-30-2002) JWW
    if (isNaN(value)) {strTemp = value; return strTemp;}

    //check for zero
    if (value == 0. ) {strTemp ="0."; return strTemp;}

    // Check for out of range
    dThisLimit = Math.pow(10, UpperSciExp);
    if (value > dThisLimit)
    {
      return "above range";
    }


    dThisLimit = - Math.pow(10, UpperSciExp);
    if (value < dThisLimit) 
    {
      return "below range";
    }
    
    // Check for negative # - save to be appended just before return
    if (value < 0) 
    {
      bNegativeNum = 1;
    } 
    else 
    {
      bNegativeNum = 0;
    }
    dAbsValue = Math.abs(value);

    // Determine magnitude of #
    // -20 is a kludge
    for (iMagnitude = UpperSciExp; iMagnitude >= -20; iMagnitude--) {
      dThisLimit = Math.pow(10, iMagnitude);
      if (dAbsValue >= dThisLimit) {
        iMagnitude++;
	// iMagnitude is the lowest power of 10 that the input value is less than
	//  e.g., value=100, iMagnitude = 3
	//  e.g., value= 99, iMagnitude = 2
	break;
      }
    }

    dMagnifier = Math.pow(10, digits - iMagnitude);
    dAdjustedVal = dAbsValue * dMagnifier;
    dRoundedVal = Math.round(dAdjustedVal);
    dLowerDecLimit = Math.pow(10, LowerDecExp);
    dUpperDecLimit = Math.pow(10, UpperDecExp);
    dInnerLimit    = Math.pow(10, LowerSciExp);

    if (dAbsValue < dInnerLimit) {
      // Narrow window around 0.0
      return ScientificNotation(digits, dRoundedVal, iMagnitude, bNegativeNum);
    } else {
      if (dLowerDecLimit <= dAbsValue && dAbsValue <= dUpperDecLimit) {
        // Use Regular Notation
        return RegularNotation(digits, dRoundedVal, iMagnitude, dMagnifier, bNegativeNum);
      } else {
        // Use Scientific Notation
        return ScientificNotation(digits, dRoundedVal, iMagnitude, bNegativeNum);
      }
    }
}

function RegularNotation(digits, dRoundedVal, iMagnitude, dMagnifier, bNegativeNum)
{
    var strTemp;
    var strNewVal; // Build string output
    var dNewVal; // After rounding to eliminate extra digits, put back to right scale
    var iAddZeros; // Number of 0's that need to be appended to show proper # of digits
    var iPad; // Loop counter for padding 0's
    var iDecimalLoc; // Location of decimal in string
    var iNonZeroLoc; // Location of 1st non-zero (and non-.) # in string
    var iNeedDigits; // How many digits do we need, including decimal and leading 0's?

      dNewVal = dRoundedVal / dMagnifier;
      strNewVal = "" + dNewVal;
      iELoc = strNewVal.indexOf("e");
      if (iELoc > -1) {
        // Number already in scientific notation
	strTemp = strNewVal.substr(0, iELoc);
        if (strTemp.indexOf(".") == -1) {
          // We need to add a ".", since "/ dMagnifier" didn't.
          strTemp = strTemp + ".";
	}
	// Ensure Mantissa has right # of digits
	// 1 for "."
        iAddZeros = digits + 1 - strTemp.length;
        for (iPad = 0; iPad < iAddZeros; iPad++) {
	  strTemp = strTemp + "0";
        }
	strTemp += strNewVal.substr(iELoc)
        if (bNegativeNum) {
          strTemp = "-" + strTemp;
        }
        return strTemp;
      }

      if (strNewVal.indexOf(".") == -1) {
        // We need to add a ".", since "/ dMagnifier" didn't.
        strNewVal = strNewVal + ".";
      }
      if (iMagnitude == 0) {
	if (strNewVal.charAt(0) == ".") {
          // In Netscape, we need to prepend a "0", since "/ dMagnifier" didn't.
          strNewVal = "0" + strNewVal;
	}
      }
      for (iNonZeroLoc = 0; iNonZeroLoc < strNewVal.length; iNonZeroLoc++) {
	if (strNewVal.charAt(iNonZeroLoc) != "0" && strNewVal.charAt(iNonZeroLoc) != ".") {
	  break;
	}
      }
      iDecimalLoc = strNewVal.indexOf(".");
      if (iDecimalLoc > iNonZeroLoc) {
        iNeedDigits = digits + 1;
      } else {
        iNeedDigits = digits;
      }

      // e.g., "0.0001" w/ 2 digits needs (5 - 6 + 2) = 1 zero appended
      iAddZeros = iNeedDigits + iNonZeroLoc - strNewVal.length;
      for (iPad = 0; iPad < iAddZeros; iPad++) {
	strNewVal = strNewVal + "0";
      }

      if (bNegativeNum)
      {
        strNewVal = "-" + strNewVal;
      }
      return strNewVal;
}

function ScientificNotation(digits, dRoundedVal, iMagnitude, bNegativeNum) 
{
  var dMantissa; // dRoundedVal in the scale of scientific notation 0.0 - 9.999...
  var strTemp;
  var iPad; // Loop counter for padding 0's
  var strMantissa; // Mantissa part of scientific notation
  var strExponent; // Exponent part of scientific notation

      dMantissa = dRoundedVal / Math.pow(10, digits - 1);
      if (dMantissa >= 10.0) 
      {
        dMantissa = dRoundedVal / Math.pow(10, digits);
        iMagnitude++;
      }
      strTemp = "" + dMantissa;
      if (strTemp.indexOf(".") == -1) 
      {
        strTemp = strTemp + ".";
      }
      // 1 for "."
      for (iPad = 0; strTemp.length < digits + 1; iPad++)
      {
	  // Pad it
	  strTemp = strTemp + "0";
      }
      // Ensure imprecision of division doesn't tag on extra digits (like "000001")
      strMantissa = strTemp.substr(0, digits + 1);
      strExponent = "e" + (iMagnitude - 1);

      if (bNegativeNum) 
      {
        strMantissa = "-" + strMantissa;
      }
      return strMantissa + strExponent;
    
}
function getDigits(number,minDigits)
{
  var digits = minDigits;

  if (100<=number&&number<1000.0) {digits=minDigits+1;}
  if (1000.0<=number&&number<10000.0) {digits=minDigits+2;}
  if (10000.0<=number&&number<100000.0) {digits=minDigits+3;}

  return digits;
}
