Number2.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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="../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; }
  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. Ext.Number = new function() {
  25. var me = this,
  26. isToFixedBroken = (0.9).toFixed() !== '1',
  27. math = Math;
  28. Ext.apply(this, {
  29. <span id='Ext-Number-method-constrain'> /**
  30. </span> * Checks whether or not the passed number is within a desired range. If the number is already within the
  31. * range it is returned, otherwise the min or max value is returned depending on which side of the range is
  32. * exceeded. Note that this method returns the constrained value but does not change the current number.
  33. * @param {Number} number The number to check
  34. * @param {Number} min The minimum number in the range
  35. * @param {Number} max The maximum number in the range
  36. * @return {Number} The constrained value if outside the range, otherwise the current value
  37. */
  38. constrain: function(number, min, max) {
  39. var x = parseFloat(number);
  40. // Watch out for NaN in Chrome 18
  41. // V8bug: http://code.google.com/p/v8/issues/detail?id=2056
  42. // Operators are faster than Math.min/max. See http://jsperf.com/number-constrain
  43. // ... and (x &lt; Nan) || (x &lt; undefined) == false
  44. // ... same for (x &gt; NaN) || (x &gt; undefined)
  45. // so if min or max are undefined or NaN, we never return them... sadly, this
  46. // is not true of null (but even Math.max(-1,null)==0 and isNaN(null)==false)
  47. return (x &lt; min) ? min : ((x &gt; max) ? max : x);
  48. },
  49. <span id='Ext-Number-method-snap'> /**
  50. </span> * Snaps the passed number between stopping points based upon a passed increment value.
  51. *
  52. * The difference between this and {@link #snapInRange} is that {@link #snapInRange} uses the minValue
  53. * when calculating snap points:
  54. *
  55. * r = Ext.Number.snap(56, 2, 55, 65); // Returns 56 - snap points are zero based
  56. *
  57. * r = Ext.Number.snapInRange(56, 2, 55, 65); // Returns 57 - snap points are based from minValue
  58. *
  59. * @param {Number} value The unsnapped value.
  60. * @param {Number} increment The increment by which the value must move.
  61. * @param {Number} minValue The minimum value to which the returned value must be constrained. Overrides the increment.
  62. * @param {Number} maxValue The maximum value to which the returned value must be constrained. Overrides the increment.
  63. * @return {Number} The value of the nearest snap target.
  64. */
  65. snap : function(value, increment, minValue, maxValue) {
  66. var m;
  67. // If no value passed, or minValue was passed and value is less than minValue (anything &lt; undefined is false)
  68. // Then use the minValue (or zero if the value was undefined)
  69. if (value === undefined || value &lt; minValue) {
  70. return minValue || 0;
  71. }
  72. if (increment) {
  73. m = value % increment;
  74. if (m !== 0) {
  75. value -= m;
  76. if (m * 2 &gt;= increment) {
  77. value += increment;
  78. } else if (m * 2 &lt; -increment) {
  79. value -= increment;
  80. }
  81. }
  82. }
  83. return me.constrain(value, minValue, maxValue);
  84. },
  85. <span id='Ext-Number-method-snapInRange'> /**
  86. </span> * Snaps the passed number between stopping points based upon a passed increment value.
  87. *
  88. * The difference between this and {@link #snap} is that {@link #snap} does not use the minValue
  89. * when calculating snap points:
  90. *
  91. * r = Ext.Number.snap(56, 2, 55, 65); // Returns 56 - snap points are zero based
  92. *
  93. * r = Ext.Number.snapInRange(56, 2, 55, 65); // Returns 57 - snap points are based from minValue
  94. *
  95. * @param {Number} value The unsnapped value.
  96. * @param {Number} increment The increment by which the value must move.
  97. * @param {Number} [minValue=0] The minimum value to which the returned value must be constrained.
  98. * @param {Number} [maxValue=Infinity] The maximum value to which the returned value must be constrained.
  99. * @return {Number} The value of the nearest snap target.
  100. */
  101. snapInRange : function(value, increment, minValue, maxValue) {
  102. var tween;
  103. // default minValue to zero
  104. minValue = (minValue || 0);
  105. // If value is undefined, or less than minValue, use minValue
  106. if (value === undefined || value &lt; minValue) {
  107. return minValue;
  108. }
  109. // Calculate how many snap points from the minValue the passed value is.
  110. if (increment &amp;&amp; (tween = ((value - minValue) % increment))) {
  111. value -= tween;
  112. tween *= 2;
  113. if (tween &gt;= increment) {
  114. value += increment;
  115. }
  116. }
  117. // If constraining within a maximum, ensure the maximum is on a snap point
  118. if (maxValue !== undefined) {
  119. if (value &gt; (maxValue = me.snapInRange(maxValue, increment, minValue))) {
  120. value = maxValue;
  121. }
  122. }
  123. return value;
  124. },
  125. <span id='Ext-Number-method-toFixed'> /**
  126. </span> * Formats a number using fixed-point notation
  127. * @param {Number} value The number to format
  128. * @param {Number} precision The number of digits to show after the decimal point
  129. */
  130. toFixed: isToFixedBroken ? function(value, precision) {
  131. precision = precision || 0;
  132. var pow = math.pow(10, precision);
  133. return (math.round(value * pow) / pow).toFixed(precision);
  134. } : function(value, precision) {
  135. return value.toFixed(precision);
  136. },
  137. <span id='Ext-Number-method-from'> /**
  138. </span> * Validate that a value is numeric and convert it to a number if necessary. Returns the specified default value if
  139. * it is not.
  140. Ext.Number.from('1.23', 1); // returns 1.23
  141. Ext.Number.from('abc', 1); // returns 1
  142. * @param {Object} value
  143. * @param {Number} defaultValue The value to return if the original value is non-numeric
  144. * @return {Number} value, if numeric, defaultValue otherwise
  145. */
  146. from: function(value, defaultValue) {
  147. if (isFinite(value)) {
  148. value = parseFloat(value);
  149. }
  150. return !isNaN(value) ? value : defaultValue;
  151. },
  152. <span id='Ext-Number-method-randomInt'> /**
  153. </span> * Returns a random integer between the specified range (inclusive)
  154. * @param {Number} from Lowest value to return.
  155. * @param {Number} to Highst value to return.
  156. * @return {Number} A random integer within the specified range.
  157. */
  158. randomInt: function (from, to) {
  159. return math.floor(math.random() * (to - from + 1) + from);
  160. }
  161. });
  162. <span id='Ext-method-num'> /**
  163. </span> * @deprecated 4.0.0 Please use {@link Ext.Number#from} instead.
  164. * @member Ext
  165. * @method num
  166. * @inheritdoc Ext.Number#from
  167. */
  168. Ext.num = function() {
  169. return me.from.apply(this, arguments);
  170. };
  171. };</pre>
  172. </body>
  173. </html>