joon hee - StandardLibrary-1.81
NAME
Number - Number Object
SYNOPSIS
var x = 512301.01317;
alert( x.toExponential() );
alert( x.toFixed(0) );
DESCRIPTION
This implements methods on all Numbers.
METHODS
Inherited
From Function.prototype: apply, call, toSource, toString, valueOf
From Object.prototype: hasOwnProperty, isPrototypeOf, propertyIsEnumerable, unwatch, watch
Constructor() Returns Number
*** Not Implementable In Standard JavaScript
valueOf() Returns Number
*** Not Implementable In Standard JavaScript
This gets you the primitive value - something standard JavaScript cannot obtain.
toExponential() Returns String
toFixed( Number decimalPoints ) Returns String
toLocaleString() Returns String
toPrecision( Number precision ) Returns String
toSource() Returns String
toString() Returns String
AUTHOR
Jhuni, <jhuni_x@yahoo.com>
COPYRIGHT
Public Domain
/*=pod
=head1 NAME
Number - Number Object
=head1 SYNOPSIS
var x = 512301.01317;
alert( x.toExponential() );
alert( x.toFixed(0) );
=head1 DESCRIPTION
This implements methods on all Numbers.
=head1 METHODS
=head2 Inherited
From Function.prototype:
apply, call, toSource, toString, valueOf
From Object.prototype:
hasOwnProperty, isPrototypeOf, propertyIsEnumerable, unwatch, watch
=cut*/
/*=pod
=head2 Constructor() Returns Number
*** Not Implementable In Standard JavaScript
=cut*/
/*=pod
=head2 valueOf() Returns Number
*** Not Implementable In Standard JavaScript
This gets you the primitive value - something standard JavaScript cannot obtain.
=cut*/
/*=pod
=head2 toExponential() Returns String
=cut*/
Number.prototype.toExponential = function() {
// get a number with only one digit that is not decimal
var points = (this.toString().split('.'))[0].length - 1;
var newValue = this.valueOf();
newValue /= Math.pow(10,points);
// Round to Point:
var decimalPoints = arguments[0];
if( typeof decimalPoints == 'undefined' ) {
decimalPoints = (this.toString().length) - 2;
if( 0 > decimalPoints ) {
decimalPoints = 0;
}
}
newValue *= Math.pow(10,decimalPoints);
newValue = Math.round(newValue);
newValue /= Math.pow(10,decimalPoints);
// return values:
return(newValue + "e+" + points);
};
/*=pod
=head2 toFixed( Number decimalPoints ) Returns String
=cut*/
Number.prototype.toFixed = function( decimalPoints ) {
if( typeof decimalPoints == 'undefined' ) {
decimalPoints = 0;
}
var len = this.toString().length;
var spaces = this.toString().split('.');
var currentDec = len - spaces[0].length - 1;
var rval = '';
if(decimalPoints > currentDec) {
rval = this.toString();
for( var i = 0; i < (decimalPoints-currentDec); i++ ) {
rval += '0';
}
} else {
rval += spaces[0];
if(decimalPoints != 0 && spaces.length != 1 ){
rval += '.';
for( var i = 0; i < decimalPoints; i++ ){
rval += spaces[1].charAt(i);
}
}
}
return rval;
};
/*=pod
=head2 toLocaleString() Returns String
=cut*/
Number.prototype.toLocaleString = function() {
return( "" + this );
};
/*=pod
=head2 toPrecision( Number precision ) Returns String
=cut*/
Number.prototype.toPrecision = function( precision ) {
// Use toString if no precision definition is produced.
if( typeof precision == 'undefined' ) {
return this.toString();
}
if( precision.toString().indexOf(".") != -1 ) {
precision = Math.floor(precision);
}
// Check for Range Errors:
if( 0 >= precision || precision >= 101 ) {
throw new RangeError('precision ' + precision + ' out of range');
}
var rval = '';
var spaces = this.toString().split('.');
var decimalPoints = 0;
if( typeof spaces[1] != 'undefined' ) { decimalPoints = spaces[1].length; }
var currentPoints = spaces[0].length + decimalPoints;
var newDecimalPoints = precision - spaces[0].length;
if( newDecimalPoints > decimalPoints ) {
var zeroCount = precision - currentPoints;
rval += this.toString()
if( decimalPoints == 0 ) { rval += '.'; }
for( var i = 0; i < zeroCount; i++ ) {
rval += '0';
}
} else {
if( newDecimalPoints > 0 ) {
rval = this.toString().substr(0,precision+1);
} else {
var value = parseInt( spaces[0] );
if( spaces[0].length == precision ) {
return( value.toString() );
}
return( value.toExponential(precision-1) );
}
}
return rval;
};
/*=pod
=head2 toSource() Returns String
=cut*/
Number.prototype.toSource = function() {
return( '(new Number("' + this.toString() + '"))' );
};
/*=pod
=head2 toString() Returns String
=cut*/
Number.prototype.toString = function() {
return( "" + this );
};
/*=pod
=head1 AUTHOR
Jhuni, <jhuni_x@yahoo.com>
=head1 COPYRIGHT
Public Domain
=cut*/