4 A collection of the Number Object methods and functions.
12 Every Math method is mirrored in the Number object, both as prototype and generic.
16 Function: Number.from {#Number:Number-from}
17 ------------------------------------
19 Returns the passed parameter as a Number, or null if not a number.
27 1. arg - (*mixed*) The argument to return as a number.
31 * (*number*) The argument as a number.
32 * (*null*) Returns null if the number cannot be converted.
36 Number.from('12') // returns 12
37 Number.from('hello') // returns null
41 Function: Number.random {#Number:Number-random}
42 ----------------------------------------
44 Returns a random integer between the two passed in values.
48 var random = Number.random(min, max);
52 1. min - (*number*) The minimum value (inclusive).
53 2. max - (*number*) The maximum value (inclusive).
57 * (*number*) A random number between min and max.
61 Number.random(5, 20); // returns a random number between 5 and 20.
65 Number method: limit {#Number:limit}
66 -----------------------------
68 Limits this number between two bounds.
72 myNumber.limit(min, max);
76 1. min - (*number*) The minimum possible value.
77 2. max - (*number*) The maximum possible value.
81 * (*number*) The number bounded between the given limits.
85 (12).limit(2, 6.5); // returns 6.5
86 (-4).limit(2, 6.5); // returns 2
87 (4.3).limit(2, 6.5); // returns 4.3
91 Number method: round {#Number:round}
92 -----------------------------
94 Returns this number rounded to the specified precision.
98 myNumber.round([precision]);
102 1. precision - (*number*, optional: defaults to 0) The number of digits after the decimal place.
106 * (number) The number, rounded.
110 - Argument may also be negative.
114 (12.45).round() // returns 12
115 (12.45).round(1) // returns 12.5
116 (12.45).round(-1) // returns 10
120 Number method: times {#Number:times}
121 -----------------------------
123 Executes the function passed in the specified number of times.
127 myNumber.times(fn[, bind]);
131 1. fn - (*function*) The function which should be executed on each iteration of the loop. This function is passed the current iteration's index.
132 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind](/Native/Function/#Function:bind).
136 (4).times(alert); // alerts "0", then "1", then "2", then "3".
140 Number method: toFloat {#Number:toFloat}
141 ---------------------------------
143 Returns this number as a float. Useful because toFloat must work on both Strings and Numbers.
151 * (*number*) The number as a float.
155 (111).toFloat(); // returns 111
156 (111.1).toFloat(); // returns 111.1
160 Number method: toInt {#Number:toInt}
161 -----------------------------
163 Returns this number as another number with the passed in base. Useful because toInt must work on both Strings and Numbers.
167 myNumber.toInt([base]);
171 1. base - (*number*, optional: defaults to 10) The base to use.
175 * (*number*) A number with the base provided.
179 (111).toInt(); // returns 111
180 (111.1).toInt(); // returns 111
181 (111).toInt(2); // returns 7
184 Math Methods {#Number-Math}
187 There are several methods available from the Math object that can be used as Number Methods.
207 (-1).abs(); // returns 1
208 (3).pow(4); // returns 81
211 [MDC Number]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Number