4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Number'>/**
19 </span> * @class Ext.Number
21 * A collection of useful static methods to deal with numbers
27 var isToFixedBroken = (0.9).toFixed() !== '1';
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
39 constrain: function(number, min, max) {
40 number = parseFloat(number);
43 number = Math.max(number, min);
46 number = Math.min(number, max);
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.
59 snap : function(value, increment, minValue, maxValue) {
63 if (!(increment && value)) {
66 m = value % increment;
69 if (m * 2 >= increment) {
70 newValue += increment;
71 } else if (m * 2 < -increment) {
72 newValue -= increment;
75 return Ext.Number.constrain(newValue, minValue, maxValue);
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
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);
90 return value.toFixed(precision);
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
97 Ext.Number.from('1.23', 1); // returns 1.23
98 Ext.Number.from('abc', 1); // returns 1
100 * @param {Object} value
101 * @param {Number} defaultValue The value to return if the original value is non-numeric
102 * @return {Number} value, if numeric, defaultValue otherwise
104 from: function(value, defaultValue) {
105 if (isFinite(value)) {
106 value = parseFloat(value);
109 return !isNaN(value) ? value : defaultValue;
115 <span id='Ext-method-num'>/**
116 </span> * @deprecated 4.0.0 Please use {@link Ext.Number#from} instead.
119 * @alias Ext.Number#from
121 Ext.num = function() {
122 return Ext.Number.from.apply(this, arguments);