/* $Id: numbers.js,v 1.1 2011/10/12 15:07:28 sap Exp $ */

/****************
*               *
*   nformat()   *
*               *
****************/
function nformat (i, p)
{
var testnum = parseFloat(i);
var num = isNaN(testnum) ? 0 : testnum;
var nstr = num.toFixed(p) + '';
x = nstr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;

    while (rgx.test(x1)) 
    {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }

return x1 + x2;
}

/********************
*                   *
*   parse_float()   *
*                   *
********************/
function parse_float (i)
{
var str = i + '';
str = str.replace(/[^0-9\.]/g, '');
var n = parseFloat(str);

return isNaN(n) ? 0 : n;
}

/*************
*            *
*   rand()   *
*            *
*************/
function rand (min, max) 
{
    if (max) 
    {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    } 
    else 
    {
    return Math.floor(Math.random() * (min + 1));
    }
}

/****************
*               *
*   roundit()   *
*               *
****************/
function roundit (Num, Places) 
{
    if (Places > 0) 
    {
        if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) 
        {
        var Rounder = Math.pow(10, Places);
        return Math.round(Num * Rounder) / Rounder;
        }
        else return Num;
    }

else return Math.round(Num);
}

