// $Header: /export/home/davec/idx/js/RCS/gnum.js,v 1.1 2003/03/05 20:31:27 davec Exp $
// Description:
// Number handling utility functions

//--------------------------------------------------------

function nRound(nValue, nPercission) {
	// check if it is a number
	if (!(bIsNum(nValue) && bIsNum(nPercission))) {
		return 0;
	}
	var nResult = Math.round(nValue * (Math.pow(10, nPercission)));
	if (nPercission > 0) {
		nResult = nResult / (Math.pow(10, nPercission));
	}
	return nResult;	
}

//--------------------------------------------------------

function sFormatNum(nValue, nDecimals, bCommas, bRound, bZeroBlank) {
	var nMyValue = Number(nValue);
	var sMyValue = "";
	
	if (!(bIsNum(nValue) && bIsNum(nDecimals))) {
		nMyValue = 0;
	}
	
	if (bZeroBlank && nMyValue == 0) {
		return "";
	}
	
	if (bRound) {
		nMyValue = nRound(nMyValue, nDecimals);
	}
	
	nMyValue = nMyValue + 0.000001;
	sMyValue = String(nMyValue);
	
	sMyValue = sMyValue.substring(0, sMyValue.indexOf(".") + Number(nDecimals) + 1); 

	if (bCommas) {
		var i = 0;
		for (i=sMyValue.indexOf("."); ((i > 3) && (sMyValue.charAt(0) != "-"))|| (i > 4 && (sMyValue.charAt(0) == "-")); i=sMyValue.indexOf(",")) {
			sMyValue = sMyValue.substring(0, (i - 3)) + "," + sMyValue.substring(i - 3);
		} 
	}
	if (nDecimals == 0) {
		sMyValue = sMyValue.substring(0, sMyValue.indexOf("."));
	}
	
	return sMyValue;
}

//--------------------------------------------------------
