class Number
Description
Extensions to the built-in Number object.
Prototype extends native JavaScript numbers in order to provide:
ObjectRangecompatibility, throughNumber#succ.- Numerical loops with
Number#times. - Simple utility methods such as
Number#toColorPartandNumber#toPaddedString. - Instance-method aliases of many functions in the
Mathnamespace.
Methods
Instance methods
-
abs #
Number#abs() ⇒ NumberReturns the absolute value of the number. Convenience method that simply calls
Math.abson this instance and returns the result. -
ceil #
Number#ceil() ⇒ NumberReturns the smallest integer greater than or equal to the number. Convenience method that simply calls
Math.ceilon this instance and returns the result. -
floor #
Number#floor() ⇒ NumberReturns the largest integer less than or equal to the number. Convenience method that simply calls
Math.flooron this instance and returns the result. -
round #
Number#round() ⇒ NumberRounds the number to the nearest integer. Convenience method that simply calls
Math.roundon this instance and returns the result. -
succ #
Number#succ() ⇒ NumberReturns the successor of the current Number, as defined by current + 1. Used to make numbers compatible with ObjectRange.
-
times #
Number#times(iterator[,context]) ⇒ Number-
iterator(Function) – An iterator function to call. -
context(Object) – An optional context (thisvalue) to use when callingiterator.
Calls
iteratorthe specified number of times, passing in a number as the first parameter. The number will be 0 on first call, 1 on second call, etc.timesreturns the number instance it was called on.Example
(3).times(alert); // -> Alerts "0", then "1", then "2"; returns 3 var obj = {count: 0, total: 0}; function add(addend) { ++this.count; this.total += addend; } (4).times(add, obj); // -> 4 obj.count; // -> 4 obj.total; // -> 6 (e.g., 0 + 1 + 2 + 3) -
-
toColorPart #
Number#toColorPart() ⇒ StringProduces a 2-digit hexadecimal representation of the number (which is therefore assumed to be in the [0..255] range, inclusive). Useful for composing CSS color strings.
Example
10.toColorPart() // -> "0a" -
toJSON #
Number#toJSON() ⇒ StringReturns a JSON string representation of the number.
-
toPaddedString #
Number#toPaddedString(length[, radix]) ⇒ String-
length(Number) – The minimum length for the resulting string. -
radix(Number) – An optional radix for the string representation, defaults to 10 (decimal).
Returns a string representation of the number padded with leading 0s so that the string's length is at least equal to
length. Takes an optionalradixargument which specifies the base to use for conversion.Examples
(13).toPaddedString(4); // -> "0013" (13).toPaddedString(2); // -> "13" (13).toPaddedString(1); // -> "13" (13).toPaddedString(4, 16) // -> "000d" (13).toPaddedString(4, 2); // -> "1101" -