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() {
|
2007-06-06 16:10:16 +00:00
|
|
|
|
return this.toPaddedString(2, 16);
|
2008-09-29 21:06:54 +00:00
|
|
|
|
}
|
2007-06-06 16:10:16 +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() {
|
2007-06-06 16:10:16 +00:00
|
|
|
|
return this + 1;
|
2008-09-29 21:06:54 +00:00
|
|
|
|
}
|
2007-06-06 16:10:16 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Number#times(iterator) -> Number
|
|
|
|
|
*
|
|
|
|
|
* Encapsulates a regular [0..n[ loop, Ruby-style.
|
|
|
|
|
**/
|
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);
|
2007-06-06 16:10:16 +00:00
|
|
|
|
return this;
|
2008-09-29 21:06:54 +00:00
|
|
|
|
}
|
2007-06-06 16:10:16 +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 string’s 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) {
|
2007-06-06 16:10:16 +00:00
|
|
|
|
var string = this.toString(radix || 10);
|
|
|
|
|
return '0'.times(length - string.length) + string;
|
2008-09-29 21:06:54 +00:00
|
|
|
|
}
|
2007-06-06 16:10:16 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** related to: Object.toJSON
|
|
|
|
|
* Number#toJSON() -> String
|
|
|
|
|
*
|
|
|
|
|
* Returns a JSON string.
|
|
|
|
|
**/
|
2008-09-29 21:06:54 +00:00
|
|
|
|
function toJSON() {
|
2007-06-06 16:10:16 +00:00
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
})());
|