prototype/src/lang/number.js

107 lines
2.5 KiB
JavaScript
Raw Normal View History

2008-12-14 04:36:59 +00:00
/** section: lang
* Number
**/
2008-09-29 21:06:54 +00:00
Object.extend(Number.prototype, (function() {
2008-12-14 04:36:59 +00:00
/**
* Number#toColorPart() -> String
*
* Produces a 2-digit hexadecimal representation of the number
* (which is therefore assumed to be in the [0..255] range).
* Useful for composing CSS color strings.
**/
2008-09-29 21:06:54 +00:00
function toColorPart() {
return this.toPaddedString(2, 16);
2008-09-29 21:06:54 +00:00
}
2008-12-14 04:36:59 +00:00
/**
* Number#succ() -> Number
*
* Returns the successor of the current Number, as defined by current + 1.
* Used to make numbers compatible with ObjectRange.
**/
2008-09-29 21:06:54 +00:00
function succ() {
return this + 1;
2008-09-29 21:06:54 +00:00
}
2008-12-14 04:36:59 +00:00
/**
* Number#times(iterator) -> Number
*
2009-02-24 02:21:02 +00:00
* Calls `iterator` the specified number of times.
* The function takes an integer as the first parameter; it will start at 0
* and be incremented after each invocation.
2008-12-14 04:36:59 +00:00
**/
2008-09-29 21:06:54 +00:00
function times(iterator, context) {
2008-05-05 13:17:08 +00:00
$R(0, this, true).each(iterator, context);
return this;
2008-09-29 21:06:54 +00:00
}
2008-12-14 04:36:59 +00:00
/**
* Number#toPaddedString(length[, radix]) -> String
*
* Converts the number into a string padded with 0s so that the strings length
* is at least equal to `length`.
* Takes an optional `radix` argument which specifies the base to use for conversion.
**/
2008-09-29 21:06:54 +00:00
function toPaddedString(length, radix) {
var string = this.toString(radix || 10);
return '0'.times(length - string.length) + string;
2008-09-29 21:06:54 +00:00
}
2008-12-14 04:36:59 +00:00
/** related to: Object.toJSON
* Number#toJSON() -> String
*
2009-02-24 02:21:02 +00:00
* Returns a JSON string representation of the number.
2008-12-14 04:36:59 +00:00
**/
2008-09-29 21:06:54 +00:00
function toJSON() {
return isFinite(this) ? this.toString() : 'null';
}
2008-09-29 21:06:54 +00:00
2008-12-14 04:36:59 +00:00
/**
* Number#abs() -> Number
*
* Returns the absolute value of the number.
**/
2008-09-29 21:06:54 +00:00
function abs() {
return Math.abs(this);
}
2008-12-14 04:36:59 +00:00
/**
* Number#round() -> Number
*
* Rounds the number to the nearest integer.
**/
2008-09-29 21:06:54 +00:00
function round() {
return Math.round(this);
}
2008-12-14 04:36:59 +00:00
/**
* Number#ceil() -> Number
*
* Returns the smallest integer greater than or equal to the number.
**/
2008-09-29 21:06:54 +00:00
function ceil() {
return Math.ceil(this);
}
2008-12-14 04:36:59 +00:00
/**
* Number#floor() -> Number
*
* Returns the largest integer less than or equal to the number.
**/
2008-09-29 21:06:54 +00:00
function floor() {
return Math.floor(this);
}
return {
toColorPart: toColorPart,
succ: succ,
times: times,
toPaddedString: toPaddedString,
toJSON: toJSON,
abs: abs,
round: round,
ceil: ceil,
floor: floor
};
})());