Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Number3.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-Number'>/**
19 </span> * @class Ext.Number
20  *
21  * A collection of useful static methods to deal with numbers
22  * @singleton
23  */
24
25 (function() {
26
27 var isToFixedBroken = (0.9).toFixed() !== '1';
28
29 Ext.Number = {
30 <span id='Ext-Number-method-constrain'>    /**
31 </span>     * Checks whether or not the passed number is within a desired range.  If the number is already within the
32      * range it is returned, otherwise the min or max value is returned depending on which side of the range is
33      * exceeded. Note that this method returns the constrained value but does not change the current number.
34      * @param {Number} number The number to check
35      * @param {Number} min The minimum number in the range
36      * @param {Number} max The maximum number in the range
37      * @return {Number} The constrained value if outside the range, otherwise the current value
38      */
39     constrain: function(number, min, max) {
40         number = parseFloat(number);
41
42         if (!isNaN(min)) {
43             number = Math.max(number, min);
44         }
45         if (!isNaN(max)) {
46             number = Math.min(number, max);
47         }
48         return number;
49     },
50
51 <span id='Ext-Number-method-snap'>    /**
52 </span>     * Snaps the passed number between stopping points based upon a passed increment value.
53      * @param {Number} value The unsnapped value.
54      * @param {Number} increment The increment by which the value must move.
55      * @param {Number} minValue The minimum value to which the returned value must be constrained. Overrides the increment..
56      * @param {Number} maxValue The maximum value to which the returned value must be constrained. Overrides the increment..
57      * @return {Number} The value of the nearest snap target.
58      */
59     snap : function(value, increment, minValue, maxValue) {
60         var newValue = value,
61             m;
62
63         if (!(increment &amp;&amp; value)) {
64             return value;
65         }
66         m = value % increment;
67         if (m !== 0) {
68             newValue -= m;
69             if (m * 2 &gt;= increment) {
70                 newValue += increment;
71             } else if (m * 2 &lt; -increment) {
72                 newValue -= increment;
73             }
74         }
75         return Ext.Number.constrain(newValue, minValue,  maxValue);
76     },
77
78 <span id='Ext-Number-method-toFixed'>    /**
79 </span>     * Formats a number using fixed-point notation
80      * @param {Number} value The number to format
81      * @param {Number} precision The number of digits to show after the decimal point
82      */
83     toFixed: function(value, precision) {
84         if (isToFixedBroken) {
85             precision = precision || 0;
86             var pow = Math.pow(10, precision);
87             return (Math.round(value * pow) / pow).toFixed(precision);
88         }
89
90         return value.toFixed(precision);
91     },
92
93 <span id='Ext-Number-method-from'>    /**
94 </span>     * Validate that a value is numeric and convert it to a number if necessary. Returns the specified default value if
95      * it is not.
96
97 Ext.Number.from('1.23', 1); // returns 1.23
98 Ext.Number.from('abc', 1); // returns 1
99
100      * @param {Mixed} value
101      * @param {Number} defaultValue The value to return if the original value is non-numeric
102      * @return {Number} value, if numeric, defaultValue otherwise
103      */
104     from: function(value, defaultValue) {
105         if (isFinite(value)) {
106             value = parseFloat(value);
107         }
108
109         return !isNaN(value) ? value : defaultValue;
110     }
111 };
112
113 })();
114
115 <span id='Ext-method-num'>/**
116 </span> * This method is deprecated, please use {@link Ext.Number#from Ext.Number.from} instead
117  *
118  * @deprecated 4.0.0 Replaced by Ext.Number.from
119  * @member Ext
120  * @method num
121  */
122 Ext.num = function() {
123     return Ext.Number.from.apply(this, arguments);
124 };</pre>
125 </body>
126 </html>