- uploading 1.1 in tags
[mootools.git] / Native / Number.js
bloba28618f345881fd183fadd468c8b916aeaf880a3
1 /*
2 Class: Number
3         A collection of The Number Object prototype methods.
4 */
6 Number.extend({
8         /*
9         Property: toInt
10                 Returns this number; useful because toInt must work on both Strings and Numbers.
11         */
13         toInt: function(){
14                 return parseInt(this);
15         },
17         /*
18         Property: toFloat
19                 Returns this number as a float; useful because toFloat must work on both Strings and Numbers.
20         */
22         toFloat: function(){
23                 return parseFloat(this);
24         },
26         /*
27         Property: limit
28                 Limits the number.
29                 
30         Arguments:
31                 min - number, minimum value
32                 max - number, maximum value
33                 
34         Returns:
35                 the number in the given limits.
36                 
37         Example:
38                 >(12).limit(2, 6.5)  // returns 6.5
39                 >(-4).limit(2, 6.5)  // returns 2
40                 >(4.3).limit(2, 6.5) // returns 4.3
41         */
43         limit: function(min, max){
44                 return Math.min(max, Math.max(min, this));
45         },
47         /*
48         Property: round
49                 Returns the number rounded to specified precision.
51         Arguments:
52                 precision - integer, number of digits after the decimal point. Can also be negative or zero (default).
54         Example:
55                 >12.45.round() // returns 12
56                 >12.45.round(1) // returns 12.5
57                 >12.45.round(-1) // returns 10
58                 
59         Returns:
60                 The rounded number.
61         */
63         round: function(precision){
64                 precision = Math.pow(10, precision || 0);
65                 return Math.round(this * precision) / precision;
66         },
68         /*
69         Property: times
70                 Executes a passed in function the specified number of times
72         Arguments:
73                 function - the function to be executed on each iteration of the loop
75         Example:
76                 >(4).times(alert);
77         */
79         times: function(fn) {
80                 for (var i = 0; i < this; i++) fn(i);
81         }
83 });