Code:
/**************************************************************************************************
Number.prototype.format(n, x)
Format a number with . and ,: 1,123.45
@param integer n: length of decimal
@param integer x: length of sections
**************************************************************************************************/
Number.prototype.format = function ( n, x ) {
var re = '\\d(?=(\\d{' + ( x || 3 ) + '})+' + ( n > 0 ? '\\.' : '$' ) + ')';
return this.toFixed( Math.max( 0, ~~n ) ).replace( new RegExp( re, 'g' ), '$&,' );
};
/**************************************************************************************************
Number.prototype.byteFormat(n, x, t)
Convert Bytes to KB/MB/GB/TB and format with . and ,: 1,123.45
@param integer n: length of decimal
@param integer x: length of sections
@param string t: [kb|mb|gb|tb] to convert numer to
**************************************************************************************************/
Number.prototype.byteFormat = function ( n, x, t ) {
var v = this;
switch ( t.toLowerCase() ) {
case "kb":
v = v / 1024;
break;
case "mb":
v = v / 1024 / 1024;
break;
case "gb":
v = v / 1024 / 1024 / 1024;
break;
case "tb":
v = v / 1024 / 1024 / 1024 / 1024;
break;
default:
}
return v.format( n, x );
}
/**************************************************************************************************
String.prototype.FillStr(rightLeft, fillWith, length, rightStr)
Fill a string.
@param string rightLeft: string: "l"=left | "r"=right
@param string fillWith: string to use to fill
@param integer length : Final string length
@param string rightStr: right string
**************************************************************************************************/
String.prototype.FillStr = function ( rightLeft, fillWith, length, rightStr ) {
var str = this;
if ( rightStr == null ) rightStr = "";
while ( str.length < ( length - rightStr.length ) )
if ( rightLeft.toLowerCase() == "l" )
str = fillWith + str;
else
str = str + fillWith;
return str + rightStr;
}
/**************************************************************************************************
String.prototype.CenterStr(length, fillWith)
Center a string.
@param integer length : Final string length
@param string fillWith: string to use to fill
**************************************************************************************************/
String.prototype.CenterStr = function ( length, fillWith ) {
var str = this;
while ( str.length < length ) {
str = fillWith + str;
if ( str.length < length )
str += fillWith;
}
return str;
}