// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// file: number.js
// desc: Number prototypes
// auth: Héctor J. Rivas torjo2k@hotmail.com
// updt: Mon Feb 28 17:22:48 UTC-0400 2005
//		 Wed Apr 20 16:33:11 UTC-0400 2005
// 		 Tue Oct 17 16:25:05 CDT 2006 The Code Project version
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// string method wrappers
Number.prototype.zp = function(n) { return this.toString().zp(n); }
Number.prototype.zt = function(n) { return this.toString().zt(n); }
Number.prototype.substr = function(n) { return this.toString().substr(n); }
Number.prototype.reverse = function() { return this.toString().reverse(); }

// number sign 'bit' (boolean)
Number.prototype.sign = function() { return this >= 0; }

// decimal digits truncation
Number.prototype.truncate   = function(n) { return Math.round(this * Math.pow(10, n)) / Math.pow(10, n); }

// fractional part of a number
Number.prototype.fractional = function()  { return parseFloat(this) - parseInt(this); }

// integer thousand separators
Number.prototype.group = function()
{
	var s = parseInt(this).reverse(), r = '';

	for (var i = 0; i < s.length; i++)
		r += (i > 0 && i % 3 == 0 ? ',' : '') + s.charAt(i);
	
	return r.reverse();
}

// format a number with n decimal digits, thousands separator and sign
// prefix can be used for currency, e.g. $ or any string
Number.prototype.format = function(prefix,n)
{
	// remember the input sign and cancel it
	var a = Math.abs(this);

	// truncate and zero-trail the fractional part
	var f = a.fractional().truncate(n).substr(2).zt(n);

	// sign + grouped integer part + dot + fractional part
	//return (' -'.substr(this.sign(), 1) + a.group() + '.' + f).trim();
	
	//fixed to use (n) for negative instead of -n
	return ('('.substr(this.sign(), 1) + prefix + a.group() + '.' + f + ')'.substr(this.sign(), 1)).trim();
}

// math cosecant, secant and cotangent
Math.csc = function(x) { return 1 / Math.sin(x); }
Math.sec = function(x) { return 1 / Math.cos(x); }
Math.cot = function(x) { return 1 / Math.tan(x); }
