/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: * ******************************************************************* * Project: Number Enchant * * Version: 1.1 * * Author: RabiatoR * ******************************************************************* * JavaScript "Number" object functionnaly enchant library * Includes: * Number Number().random( Number min_or_max [, Number max] ); * Number Number().module( void ); * Boolean Number().isNegative( void ); * Number Number().floor( void ); * Number Number().ceil( void ); * Number Number().round( void ); * Number Number().sqrt( void ); * Number Number().pow( Number expanent ); * Number Number().length( [Boolean negativeSymbolIncludes = false] ); * String Number().toHex( [Number length = 8] ); * String Number().float( Number length [, String type = round] ); * String Number().px(); * * Changelog: * [1.1] * Number().float - added. * Number().px - added. * [1.01] * Syntax reformatted. * [1.0] * First release. * * */ /**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**/ /* * Syntax: Number().float( Number length [, String type = "round"] ); * Return: Number; * Failur: false; *****************************************************************/ if( typeof( Number.prototype.float ) == 'undefined' ){ Number.prototype.float = function( length, type ){ if( typeof( length ) != 'number' ){ return false; } if( typeof( type ) != 'string' ){ type = "round"; } var retn = this; var r = (10).pow( length ); retn *= r; retn = retn[type](); return retn / r; } } /* * Syntax: Number().px(); * Return: String; * Failur: false; * Requar: Number().float; *****************************************************************/ if( typeof( Number.prototype.px ) == 'undefined' ){ Number.prototype.px = function( length, type ){ var px = this; if( px != parseInt( px ) ){ px = px.float(1); } return px +"px"; } } /* * Syntax: Number().random( Number min_or_max [, Number max] ); * Return: Number; * Failur: false; *****************************************************************/ if( typeof( Number.prototype.random ) == 'undefined' ){ Number.prototype.random = function( min_or_max, max ){ var min = parseInt( min_or_max, 10 ); if( typeof( min_or_max ) == 'undefined' ){ min = 0; max = 2147483647; }else if( typeof( max ) == 'undefined' ){ min = 0; max = min_or_max; } max = parseInt( max, 10 ); if( isNaN( min ) || isNaN( max ) ){ return false; } return Math.floor( Math.random() * ( max - min + 1 ) ) + min; } } /* * Syntax: Number().module( void ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.module ) == 'undefined' ){ Number.prototype.module = function(){ return (this < 0)? this * -1 : this; } } /* * Syntax: Number().isNegative( void ); * Return: Boolean; *****************************************************************/ if( typeof( Number.prototype.isNegative ) == 'undefined' ){ Number.prototype.isNegative = function(){ return (this < 0); } } /* * Syntax: Number().floor( void ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.floor ) == 'undefined' ){ Number.prototype.floor = function(){ return Math.floor( this ); } } /* * Syntax: Number().ceil( void ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.ceil ) == 'undefined' ){ Number.prototype.ceil = function(){ return Math.ceil( this ); } } /* * Syntax: Number().round( void ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.round ) == 'undefined' ){ Number.prototype.round = function(){ return Math.round( this ); } } /* * Syntax: Number().sqrt( void ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.sqrt ) == 'undefined' ){ Number.prototype.sqrt = function(){ return Math.sqrt( this ); } } /* * Syntax: Number().pow( Number expanent ); * Return: Number; * Failur: false; *****************************************************************/ if( typeof( Number.prototype.pow ) == 'undefined' ){ Number.prototype.pow = function( exponent ){ if( typeof( exponent ) != 'number' ){ return false; } return Math.pow( this, exponent ); } } /* * Syntax: Number().length( [Boolean negativeSymbolIncludes = false] ); * Return: Number; *****************************************************************/ if( typeof( Number.prototype.length ) == 'undefined' ){ Number.prototype.length = function( negativeSymbolIncludes ){ var retn = this.toString().length; if( this < 0 && !negativeSymbolIncludes ){ retn--; } return retn; } } /* * Syntax: Number().toHex( [Number length = 8] ); * Return: String; * Failur: false; *****************************************************************/ if( typeof( Number.prototype.toHex ) == 'undefined' ){ Number.prototype.toHex = function( length ){ if( typeof( length ) != 'number' ){ length = 8; }else{ length = parseInt( length ); } var max = Math.pow( 16, length ), min = max * -1, val = (this < 0)? parseInt(this) * -1 : parseInt(this); max--; if( this > max || this < min ){ return false; } if( this < 0 ){ val = max - val + 1; } var retn = val.toString( 16 ); retn = Array(length - retn.length + 1).join("0") + retn; return retn; } }