Number

Prototype extends native JavaScript numbers in order to provide:

What becomes possible


$R(1, 10).each(function(index) {
  // This gets invoked with index from 1 to 10, inclusive
});

(5).times(function(n) {
  // This gets invoked with index from 0 to 5, *exclusive*
  // The parentheses are due to JS syntax, if we did not use a literal, they'd be superfluous
});

(128).toColorPart()
// -> '80'

(10).toColorPart()
// -> '0a'

'#' + [128, 10, 16].invoke('toColorPart').join('')
// -> '#800a10'

Methods

abs
1.6.0

abs() -> Number

Returns the absolute value of the number.

ceil
1.6.0

ceil() -> Number

Returns the smallest integer greater than or equal to the number.

floor
1.6.0

floor() -> Number

Returns the largest integer less than or equal to the number.

round
1.6.0

round() -> Number

Rounds the number to the nearest integer.

succ

succ() -> Number

Returns the successor of the current Number, as defined by current + 1. Used to make numbers compatible with ObjectRange.

times

times(iterator) -> Number

Encapsulates a regular [0..n[ loop, Ruby-style.

toColorPart

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.

toJSON
1.5.1

toJSON() -> String

Returns a JSON string.

toPaddedString
1.5.1

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.