Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / core / src / lang / Number.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.Number
17  *
18  * A collection of useful static methods to deal with numbers
19  * @singleton
20  */
21
22 (function() {
23
24 var isToFixedBroken = (0.9).toFixed() !== '1';
25
26 Ext.Number = {
27     /**
28      * Checks whether or not the passed number is within a desired range.  If the number is already within the
29      * range it is returned, otherwise the min or max value is returned depending on which side of the range is
30      * exceeded. Note that this method returns the constrained value but does not change the current number.
31      * @param {Number} number The number to check
32      * @param {Number} min The minimum number in the range
33      * @param {Number} max The maximum number in the range
34      * @return {Number} The constrained value if outside the range, otherwise the current value
35      */
36     constrain: function(number, min, max) {
37         number = parseFloat(number);
38
39         if (!isNaN(min)) {
40             number = Math.max(number, min);
41         }
42         if (!isNaN(max)) {
43             number = Math.min(number, max);
44         }
45         return number;
46     },
47
48     /**
49      * Snaps the passed number between stopping points based upon a passed increment value.
50      * @param {Number} value The unsnapped value.
51      * @param {Number} increment The increment by which the value must move.
52      * @param {Number} minValue The minimum value to which the returned value must be constrained. Overrides the increment..
53      * @param {Number} maxValue The maximum value to which the returned value must be constrained. Overrides the increment..
54      * @return {Number} The value of the nearest snap target.
55      */
56     snap : function(value, increment, minValue, maxValue) {
57         var newValue = value,
58             m;
59
60         if (!(increment && value)) {
61             return value;
62         }
63         m = value % increment;
64         if (m !== 0) {
65             newValue -= m;
66             if (m * 2 >= increment) {
67                 newValue += increment;
68             } else if (m * 2 < -increment) {
69                 newValue -= increment;
70             }
71         }
72         return Ext.Number.constrain(newValue, minValue,  maxValue);
73     },
74
75     /**
76      * Formats a number using fixed-point notation
77      * @param {Number} value The number to format
78      * @param {Number} precision The number of digits to show after the decimal point
79      */
80     toFixed: function(value, precision) {
81         if (isToFixedBroken) {
82             precision = precision || 0;
83             var pow = Math.pow(10, precision);
84             return (Math.round(value * pow) / pow).toFixed(precision);
85         }
86
87         return value.toFixed(precision);
88     },
89
90     /**
91      * Validate that a value is numeric and convert it to a number if necessary. Returns the specified default value if
92      * it is not.
93
94 Ext.Number.from('1.23', 1); // returns 1.23
95 Ext.Number.from('abc', 1); // returns 1
96
97      * @param {Object} value
98      * @param {Number} defaultValue The value to return if the original value is non-numeric
99      * @return {Number} value, if numeric, defaultValue otherwise
100      */
101     from: function(value, defaultValue) {
102         if (isFinite(value)) {
103             value = parseFloat(value);
104         }
105
106         return !isNaN(value) ? value : defaultValue;
107     }
108 };
109
110 })();
111
112 /**
113  * @deprecated 4.0.0 Please use {@link Ext.Number#from} instead.
114  * @member Ext
115  * @method num
116  * @alias Ext.Number#from
117  */
118 Ext.num = function() {
119     return Ext.Number.from.apply(this, arguments);
120 };