Number.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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='Number'>/**
  19. </span> * @class Number
  20. *
  21. * Creates a wrapper object to allow you to work with numerical values.
  22. *
  23. * The primary uses for the `Number` object are:
  24. *
  25. * If the argument cannot be converted into a number, it returns `NaN`.
  26. *
  27. * In a non-constructor context (i.e., without the `new` operator), `Number` can
  28. * be used to perform a type conversion.
  29. *
  30. * # Using the `Number` object to assign values to numeric variables
  31. *
  32. * The following example uses the `Number` object's properties to assign values to
  33. * several numeric variables:
  34. *
  35. * biggestNum = Number.MAX_VALUE;
  36. * smallestNum = Number.MIN_VALUE;
  37. * infiniteNum = Number.POSITIVE_INFINITY;
  38. * negInfiniteNum = Number.NEGATIVE_INFINITY;
  39. * notANum = Number.NaN;
  40. *
  41. * # Using `Number` to convert a `Date` object
  42. *
  43. * The following example converts the `Date` object to a numerical value using
  44. * `Number` as a function:
  45. *
  46. * var d = new Date(&quot;December 17, 1995 03:24:00&quot;);
  47. * print(Number(d));
  48. *
  49. * This displays &quot;819199440000&quot;.
  50. *
  51. * The following example converts the Date object to a numerical value using
  52. * `Number` as a function:
  53. *
  54. * &lt;div class=&quot;notice&quot;&gt;
  55. * Documentation for this class comes from &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number&quot;&gt;MDN&lt;/a&gt;
  56. * and is available under &lt;a href=&quot;http://creativecommons.org/licenses/by-sa/2.0/&quot;&gt;Creative Commons: Attribution-Sharealike license&lt;/a&gt;.
  57. * &lt;/div&gt;
  58. */
  59. <span id='Number-method-constructor'>/**
  60. </span> * @method constructor
  61. * Creates new Number object.
  62. * @param value
  63. * The numeric value of the object being created.
  64. */
  65. //Properties
  66. <span id='Number-static-property-MAX_VALUE'>/**
  67. </span> * @property {Number} MAX_VALUE
  68. * @static
  69. * The largest positive representable number. The largest negative representable
  70. * number is `-MAX_VALUE`.
  71. *
  72. * The `MAX_VALUE` property has a value of approximately 1.79E+308. Values larger than `MAX_VALUE` are
  73. * represented as `&quot;Infinity&quot;`.
  74. *
  75. * Because `MAX_VALUE` is a static property of `Number`, you always use it as `Number.MAX_VALUE`,
  76. * rather than as a property of a `Number` object you created.
  77. *
  78. * The following code multiplies two numeric values. If the result is less than or equal to
  79. * `MAX_VALUE`, the `func1` function is called; otherwise, the `func2` function is called.
  80. *
  81. * if (num1 * num2 &lt;= Number.MAX_VALUE)
  82. * func1();
  83. * else
  84. * func2();
  85. */
  86. <span id='Number-static-property-MIN_VALUE'>/**
  87. </span> * @property {Number} MIN_VALUE
  88. * @static
  89. * The smallest positive representable number -- that is, the positive number
  90. * closest to zero (without actually being zero). The smallest negative
  91. * representable number is `-MIN_VALUE`.
  92. *
  93. * The `MIN_VALUE` property is the number closest to 0, not the most negative number, that JavaScript
  94. * can represent.
  95. *
  96. * `MIN_VALUE` has a value of approximately 5e-324. Values smaller than `MIN_VALUE` (&quot;underflow
  97. * values&quot;) are converted to 0.
  98. *
  99. * Because `MIN_VALUE` is a static property of `Number`, you always use it as `Number.MIN_VALUE`,
  100. * rather than as a property of a `Number` object you created.
  101. *
  102. * The following code divides two numeric values. If the result is greater than or equal to
  103. * `MIN_VALUE`, the `func1` function is called; otherwise, the `func2` function is called.
  104. *
  105. * if (num1 / num2 &gt;= Number.MIN_VALUE)
  106. * func1()
  107. * else
  108. * func2()
  109. */
  110. <span id='Number-static-property-NaN'>/**
  111. </span> * @property {Number} NaN
  112. * @static
  113. * Special &quot;not a number&quot; value.
  114. */
  115. <span id='Number-property-NEGATIVE_INFINITY'>/**
  116. </span> * @property {Number} NEGATIVE_INFINITY
  117. * Special value representing negative infinity; returned on overflow.
  118. *
  119. * The value of `Number.NEGATIVE_INFINITY` is the same as the negative value of the global object's
  120. * Infinity property.
  121. *
  122. * This value behaves slightly differently than mathematical infinity:
  123. *
  124. * * Any positive value, including POSITIVE_INFINITY, multiplied by NEGATIVE_INFINITY is NEGATIVE_INFINITY.
  125. * * Any negative value, including NEGATIVE_INFINITY, multiplied by NEGATIVE_INFINITY is
  126. * POSITIVE_INFINITY.
  127. * * Zero multiplied by NEGATIVE_INFINITY is NaN.
  128. * * NaN multiplied by NEGATIVE_INFINITY is NaN.
  129. * * NEGATIVE_INFINITY, divided by any negative value except NEGATIVE_INFINITY, is
  130. * POSITIVE_INFINITY.
  131. * * NEGATIVE_INFINITY, divided by any positive value except POSITIVE_INFINITY, is
  132. * NEGATIVE_INFINITY.
  133. * * NEGATIVE_INFINITY, divided by either NEGATIVE_INFINITY or POSITIVE_INFINITY, is NaN.
  134. * * Any number divided by NEGATIVE_INFINITY is Zero.
  135. *
  136. * Several JavaScript methods (such as the `Number` constructor, `parseFloat`, and `parseInt`) return
  137. * `NaN` if the value specified in the parameter is significantly lower than `Number.MIN_VALUE`.
  138. *
  139. * You might use the `Number.NEGATIVE_INFINITY` property to indicate an error condition that returns a
  140. * finite number in case of success. Note, however, that `isFinite` would be more appropriate in such
  141. * a case.
  142. *
  143. * In the following example, the variable smallNumber is assigned a value that is smaller than the
  144. * minimum value. When the `if` statement executes, `smallNumber` has the value `&quot;-Infinity&quot;`, so
  145. * `smallNumber` is set to a more manageable value before continuing.
  146. *
  147. * var smallNumber = (-Number.MAX_VALUE) * 2
  148. * if (smallNumber == Number.NEGATIVE_INFINITY) {
  149. * smallNumber = returnFinite();
  150. * }
  151. */
  152. <span id='Number-property-POSITIVE_INFINITY'>/**
  153. </span> * @property {Number} POSITIVE_INFINITY
  154. * Special value representing infinity; returned on overflow.
  155. *
  156. * The value of `Number.POSITIVE_INFINITY` is the same as the value of the global object's Infinity
  157. * property.
  158. *
  159. * This value behaves slightly differently than mathematical infinity:
  160. *
  161. * * Any positive value, including POSITIVE_INFINITY, multiplied by POSITIVE_INFINITY is
  162. * POSITIVE_INFINITY.
  163. * * Any negative value, including NEGATIVE_INFINITY, multiplied by POSITIVE_INFINITY is
  164. * NEGATIVE_INFINITY.
  165. * * Zero multiplied by POSITIVE_INFINITY is NaN.
  166. * * NaN multiplied by POSITIVE_INFINITY is NaN.
  167. * * POSITIVE_INFINITY, divided by any negative value except NEGATIVE_INFINITY, is
  168. * NEGATIVE_INFINITY.
  169. * * POSITIVE_INFINITY, divided by any positive value except POSITIVE_INFINITY, is
  170. * POSITIVE_INFINITY.
  171. * * POSITIVE_INFINITY, divided by either NEGATIVE_INFINITY or POSITIVE_INFINITY, is NaN.
  172. * * Any number divided by POSITIVE_INFINITY is Zero.
  173. *
  174. * Several JavaScript methods (such as the `Number` constructor, `parseFloat`, and `parseInt`) return
  175. * `NaN` if the value specified in the parameter is significantly higher than `Number.MAX_VALUE`.
  176. *
  177. * You might use the `Number.POSITIVE_INFINITY` property to indicate an error condition that returns a
  178. * finite number in case of success. Note, however, that `isFinite` would be more appropriate in such
  179. * a case.
  180. *
  181. * In the following example, the variable `bigNumber` is assigned a value that is larger than the
  182. * maximum value. When the if statement executes, `bigNumber` has the value &quot;Infinity&quot;, so `bigNumber`
  183. * is set to a more manageable value before continuing.
  184. *
  185. * var bigNumber = Number.MAX_VALUE * 2
  186. * if (bigNumber == Number.POSITIVE_INFINITY) {
  187. * bigNumber = returnFinite();
  188. * }
  189. */
  190. //Methods
  191. <span id='Number-method-toExponential'>/**
  192. </span> * @method toExponential
  193. * Returns a string representing the number in exponential notation.
  194. *
  195. * A string representing a `Number` object in exponential notation with one digit before the decimal
  196. * point, rounded to `fractionDigits` digits after the decimal point. If the `fractionDigits` argument
  197. * is omitted, the number of digits after the decimal point defaults to the number of digits necessary
  198. * to represent the value uniquely.
  199. *
  200. * If you use the `toExponential` method for a numeric literal and the numeric literal has no exponent
  201. * and no decimal point, leave a space before the dot that precedes the method call to prevent the dot
  202. * from being interpreted as a decimal point.
  203. *
  204. * If a number has more digits that requested by the `fractionDigits` parameter, the number is rounded
  205. * to the nearest number represented by `fractionDigits` digits. See the discussion of rounding in the
  206. * description of the `toFixed` method, which also applies to `toExponential`.
  207. *
  208. * var num=77.1234;
  209. *
  210. * alert(&quot;num.toExponential() is &quot; + num.toExponential()); //displays 7.71234e+1
  211. *
  212. * alert(&quot;num.toExponential(4) is &quot; + num.toExponential(4)); //displays 7.7123e+1
  213. *
  214. * alert(&quot;num.toExponential(2) is &quot; + num.toExponential(2)); //displays 7.71e+1
  215. *
  216. * alert(&quot;77.1234.toExponential() is &quot; + 77.1234.toExponential()); //displays 7.71234e+1
  217. *
  218. * alert(&quot;77 .toExponential() is &quot; + 77 .toExponential()); //displays 7.7e+1
  219. *
  220. * @param {Number} fractionDigits An integer specifying the number of digits after the decimal
  221. * point. Defaults to as many digits as necessary to specify the number.
  222. * @return {String} Exponential notation of number.
  223. */
  224. <span id='Number-method-toFixed'>/**
  225. </span> * @method toFixed
  226. * Returns a string representing the number in fixed-point notation.
  227. *
  228. * @return {String} A string representation of `number` that does not use
  229. * exponential notation and has exactly `digits` digits after the decimal place.
  230. * The number is rounded if necessary, and the fractional part is padded with
  231. * zeros if necessary so that it has the specified length. If `number` is greater
  232. * than 1e+21, this method simply calls `Number.toString()` and returns a string
  233. * in exponential notation.
  234. *
  235. * @param {Number} digits The number of digits to appear after the decimal point; this may be a
  236. * value between 0 and 20, inclusive, and implementations may optionally support a larger range of
  237. * values. If this argument is omitted, it is treated as 0.
  238. */
  239. <span id='Number-method-toLocaleString'>/**
  240. </span> * @method toLocaleString
  241. * Returns a human readable string representing the number using the locale of the
  242. * environment. Overrides the `Object.prototype.toLocaleString` method.
  243. *
  244. * This method available to numbers will convert the number into a string which is suitable for
  245. * presentation in the given locale.
  246. *
  247. * var number = 3500
  248. * console.log(number.toLocaleString()); // Displays &quot;3,500&quot; in English locale
  249. *
  250. * @return {String} String representing the number.
  251. */
  252. <span id='Number-method-toPrecision'>/**
  253. </span> * @method toPrecision
  254. * Returns a string representing the number to a specified precision in fixed-
  255. * point or exponential notation.
  256. *
  257. * A string representing a `Number` object in fixed-point or
  258. * exponential notation rounded to precision significant digits. See the
  259. * discussion of rounding in the description of the `toFixed` method, which also
  260. * applies to `toPrecision`.
  261. *
  262. * If the precision argument is omitted, behaves as Number.toString. If it is a
  263. * non-integer value, it is rounded to the nearest integer. After rounding, if
  264. * that value is not between 1 and 100 (inclusive), a RangeError is thrown.
  265. *
  266. * @param {Number} precision An integer specifying the number of significant digits.
  267. * @return {String} String that represents `Number` object.
  268. */
  269. <span id='Number-method-toString'>/**
  270. </span> * @method toString
  271. * Returns a string representing the specified object. Overrides the
  272. * `Object.prototype.toString` method.
  273. *
  274. * The `Number` object overrides the `toString` method of the `Object` object; it does not inherit
  275. * `Object.toString`. For `Number` objects, the toString method returns a string representation of the
  276. * object in the specified radix.
  277. *
  278. * The `toString` method parses its first argument, and attempts to return a string representation in
  279. * the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals
  280. * greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
  281. *
  282. * If `toString` is given a radix not between 2 and 36, an exception is thrown.
  283. *
  284. * If the radix is not specified, JavaScript assumes the preferred radix is 10.
  285. *
  286. * var count = 10;
  287. * print(count.toString()); // displays &quot;10&quot;
  288. * print((17).toString()); // displays &quot;17&quot;
  289. *
  290. * var x = 7;
  291. * print(x.toString(2)); // displays &quot;111&quot;
  292. *
  293. * @param {Number} radix An integer between 2 and 36 specifying the base to use for representing
  294. * numeric values.
  295. * @return {String} The number represented as a string.
  296. */
  297. <span id='Number-method-valueOf'>/**
  298. </span> * @method valueOf
  299. * Returns the primitive value of the specified object. Overrides the
  300. * `Object.prototype.valueOf` method.
  301. *
  302. * The `valueOf` method of `Number` returns the primitive value of a `Number` object as a number data
  303. * type.
  304. *
  305. * This method is usually called internally by JavaScript and not explicitly in code.
  306. *
  307. * var x = new Number();
  308. * print(x.valueOf()); // prints &quot;0&quot;
  309. *
  310. * @return {Number} The primitive value of the number.
  311. */</pre>
  312. </body>
  313. </html>