Upgrade to ExtJS 4.0.1 - Released 05/18/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 current 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-toFixed'>    /**
52 </span>     * Formats a number using fixed-point notation
53      * @param {Number} value The number to format
54      * @param {Number} precision The number of digits to show after the decimal point
55      */
56     toFixed: function(value, precision) {
57         if (isToFixedBroken) {
58             precision = precision || 0;
59             var pow = Math.pow(10, precision);
60             return (Math.round(value * pow) / pow).toFixed(precision);
61         }
62
63         return value.toFixed(precision);
64     },
65
66 <span id='Ext-Number-method-from'>    /**
67 </span>     * Validate that a value is numeric and convert it to a number if necessary. Returns the specified default value if
68      * it is not.
69
70 Ext.Number.from('1.23', 1); // returns 1.23
71 Ext.Number.from('abc', 1); // returns 1
72
73      * @param {Mixed} value
74      * @param {Number} defaultValue The value to return if the original value is non-numeric
75      * @return {Number} value, if numeric, defaultValue otherwise
76      */
77     from: function(value, defaultValue) {
78         if (isFinite(value)) {
79             value = parseFloat(value);
80         }
81
82         return !isNaN(value) ? value : defaultValue;
83     }
84 };
85
86 })();
87
88 <span id='Ext-method-num'>/**
89 </span> * This method is deprecated, please use {@link Ext.Number#from Ext.Number.from} instead
90  *
91  * @deprecated 4.0.0 Replaced by Ext.Number.from
92  * @member Ext
93  * @method num
94  */
95 Ext.num = function() {
96     return Ext.Number.from.apply(this, arguments);
97 };</pre>
98 </body>
99 </html>