Merge pull request #2789 from SergioCrisostomo/upgrade-karma-sauce-launcher
[mootools.git] / Docs / Types / Number.md
blobab40acbde8d35706c3e1f2462966194440232873
1 Type: Number {#Number}
2 ======================
4 A collection of the Number Object methods and functions.
6 ### See Also:
8 - [MDN Number][]
10 ### Notes:
12 Every Math method is mirrored in the Number object, both as prototype and generic.
16 Function: Number.convert {#Number:Number:convert}
17 ------------------------------------
19 Returns the passed parameter as a Number, or null if not a number.
21 ### Syntax:
23         Number.convert(arg);
25 ### Arguments:
27 1. arg - (*mixed*) The argument to return as a number.
29 ### Returns:
31 * (*number*) The argument as a number.
32 * (*null*) Returns null if the number cannot be converted.
34 ### Example:
36         Number.convert('12')            // returns 12
37         Number.convert('hello') // returns null
41 Function: Number.random {#Number:Number-random}
42 ----------------------------------------
44 Returns a random integer between the two passed in values.
46 ### Syntax:
48         var random = Number.random(min, max);
50 ### Arguments:
52 1. min - (*number*) The minimum value (inclusive).
53 2. max - (*number*) The maximum value (inclusive).
55 ### Returns:
57 * (*number*) A random number between min and max.
59 ### Example:
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.
70 ### Syntax:
72         myNumber.limit(min, max);
74 ### Arguments:
76 1. min - (*number*) The minimum possible value.
77 2. max - (*number*) The maximum possible value.
79 ### Returns:
81 * (*number*) The number bounded between the given limits.
83 ### Examples:
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.
96 ### Syntax:
98         myNumber.round([precision]);
100 ### Arguments:
102 1. precision - (*number*, optional: defaults to 0) The number of digits after the decimal place.
104 ### Returns:
106 * (number) The number, rounded.
108 ### Notes:
110 - Argument may also be negative.
112 ### Examples:
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.
125 ### Syntax:
127         myNumber.times(fn[, bind]);
129 ### Arguments:
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][].
134 ### Examples:
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.
145 ### Syntax:
147         myNumber.toFloat();
149 ### Returns:
151 * (*number*) The number as a float.
153 ### Examples:
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.
165 ### Syntax:
167         myNumber.toInt([base]);
169 ### Arguments:
171 1. base - (*number*, optional: defaults to 10) The base to use.
173 ### Returns:
175 * (*number*) A number with the base provided.
177 ### Examples:
179         (111).toInt(); // returns 111
180         (111.1).toInt(); // returns 111
181         (111).toInt(2); // returns 7
184 Math Methods {#Number-Math}
185 --------------------
187 There are several methods available from the Math object that can be used as Number Methods.
189 - abs
190 - acos
191 - asin
192 - atan2
193 - ceil
194 - cos
195 - exp
196 - floor
197 - log
198 - max
199 - min
200 - pow
201 - sin
202 - sqrt
203 - tan
205 ### Examples:
207         (-1).abs(); // returns 1
208         (3).pow(4); // returns 81
211 [Function:bind]: /core/Types/Function/#Function:bind
212 [MDN Number]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Number