{"id":2127,"date":"2025-02-28T23:58:02","date_gmt":"2025-02-28T15:58:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2127"},"modified":"2025-02-28T23:58:02","modified_gmt":"2025-02-28T15:58:02","slug":"javascript-number-%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/28\/javascript-number-%e5%af%b9%e8%b1%a1\/","title":{"rendered":"JavaScript\u00a0Number\u00a0\u5bf9\u8c61"},"content":{"rendered":"\n<p><code>JavaScript Number<\/code> \u5bf9\u8c61\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u503c\u7684\u5bf9\u8c61\u7c7b\u578b\u3002\u867d\u7136 JavaScript \u4e2d\u7684\u6570\u5b57\uff08\u65e0\u8bba\u662f\u6574\u6570\u8fd8\u662f\u6d6e\u70b9\u6570\uff09\u672c\u8d28\u4e0a\u662f\u539f\u59cb\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>Number<\/code> \u5bf9\u8c61\u6765\u5904\u7406\u6570\u5b57\u7684\u76f8\u5173\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u521b\u5efa Number \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u5b57\u76f4\u63a5\u4ee5\u5b57\u9762\u91cf\u7684\u5f62\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>Number<\/code> \u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a <code>Number<\/code> \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const num1 = new Number(10);  \/\/ \u4f7f\u7528 Number \u6784\u9020\u51fd\u6570\u521b\u5efa Number \u5bf9\u8c61\nconsole.log(num1);  \/\/ \u8f93\u51fa\uff1aNumber { 10 }\n\nconst num2 = 10;  \/\/ \u76f4\u63a5\u4f7f\u7528\u6570\u5b57\u5b57\u9762\u91cf\nconsole.log(num2);  \/\/ \u8f93\u51fa\uff1a10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Number \u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p><code>Number<\/code> \u5bf9\u8c61\u4e0d\u4ec5\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u5b57\u7c7b\u578b\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9759\u6001\u5c5e\u6027\u548c\u5b9e\u4f8b\u65b9\u6cd5\u6765\u5e2e\u52a9\u4f60\u5904\u7406\u6570\u5b57\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9759\u6001\u5c5e\u6027<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Number.MAX_VALUE<\/code>\uff1a\u8868\u793a JavaScript \u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u3002<\/li>\n\n\n\n<li><code>Number.MIN_VALUE<\/code>\uff1a\u8868\u793a JavaScript \u53ef\u4ee5\u8868\u793a\u7684\u6700\u5c0f\u6b63\u6570\uff08\u5927\u4e8e\u96f6\uff09\u3002<\/li>\n\n\n\n<li><code>Number.NaN<\/code>\uff1a\u8868\u793a \u201cNot-A-Number\u201d \u503c\u3002<\/li>\n\n\n\n<li><code>Number.POSITIVE_INFINITY<\/code>\uff1a\u8868\u793a\u6b63\u65e0\u7a77\u5927\u3002<\/li>\n\n\n\n<li><code>Number.NEGATIVE_INFINITY<\/code>\uff1a\u8868\u793a\u8d1f\u65e0\u7a77\u5927\u3002<\/li>\n\n\n\n<li><code>Number.EPSILON<\/code>\uff1a\u8868\u793a\u6d6e\u70b9\u6570\u4e4b\u95f4\u7684\u6700\u5c0f\u5dee\u5f02\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(Number.MAX_VALUE);  \/\/ 1.7976931348623157e+308\nconsole.log(Number.MIN_VALUE);  \/\/ 5e-324\nconsole.log(Number.NaN);        \/\/ NaN\nconsole.log(Number.POSITIVE_INFINITY);  \/\/ Infinity\nconsole.log(Number.NEGATIVE_INFINITY);  \/\/ -Infinity\nconsole.log(Number.EPSILON);    \/\/ 2.220446049250313e-16<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u4f8b\u65b9\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>toString([radix])<\/code>\uff1a\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6307\u5b9a\u8fdb\u5236\uff08\u9ed8\u8ba4\u4e3a 10 \u8fdb\u5236\uff09\u3002<\/li>\n\n\n\n<li><code>toFixed(digits)<\/code>\uff1a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><code>toPrecision(precision)<\/code>\uff1a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7cbe\u5ea6\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><code>toExponential([digits])<\/code>\uff1a\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u6307\u6570\u5f62\u5f0f\u3002<\/li>\n\n\n\n<li><code>valueOf()<\/code>\uff1a\u8fd4\u56de <code>Number<\/code> \u5bf9\u8c61\u7684\u539f\u59cb\u6570\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>toString()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p><code>toString()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u5b57\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u8fdb\u800c\u652f\u6301\u4e0d\u540c\u7684\u8fdb\u5236\u8868\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const num = 255;\nconsole.log(num.toString());        \/\/ \"255\" \uff08\u9ed8\u8ba4 10 \u8fdb\u5236\uff09\nconsole.log(num.toString(2));       \/\/ \"11111111\" \uff082 \u8fdb\u5236\uff09\nconsole.log(num.toString(16));      \/\/ \"ff\" \uff0816 \u8fdb\u5236\uff09\nconsole.log(num.toString(8));       \/\/ \"377\" \uff088 \u8fdb\u5236\uff09<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>toFixed()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p><code>toFixed()<\/code> \u65b9\u6cd5\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const num1 = 123.456;\nconsole.log(num1.toFixed(2));  \/\/ \"123.46\"\uff08\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff09\n\nconst num2 = 1.2345;\nconsole.log(num2.toFixed(4));  \/\/ \"1.2345\"\uff08\u4fdd\u7559\u56db\u4f4d\u5c0f\u6570\uff09<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>toFixed()<\/code> \u65b9\u6cd5\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>toPrecision()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p><code>toPrecision()<\/code> \u65b9\u6cd5\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u7cbe\u5ea6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const num = 123.456;\nconsole.log(num.toPrecision(4));  \/\/ \"123.5\"\uff08\u4fdd\u7559 4 \u4f4d\u6709\u6548\u6570\u5b57\uff09\nconsole.log(num.toPrecision(6));  \/\/ \"123.456\"\uff08\u4fdd\u7559 6 \u4f4d\u6709\u6548\u6570\u5b57\uff09<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>toExponential()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p><code>toExponential()<\/code> \u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u6307\u6570\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const num = 123456;\nconsole.log(num.toExponential());     \/\/ \"1.23456e+5\"\nconsole.log(num.toExponential(2));    \/\/ \"1.23e+5\"\uff08\u4fdd\u7559\u4e24\u4f4d\u6709\u6548\u6570\u5b57\uff09<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>valueOf()<\/code> \u65b9\u6cd5<\/h4>\n\n\n\n<p><code>valueOf()<\/code> \u8fd4\u56de\u6570\u5b57\u5bf9\u8c61\u7684\u539f\u59cb\u503c\uff08\u5373\u6570\u503c\u672c\u8eab\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const numObj = new Number(100);\nconsole.log(numObj.valueOf());  \/\/ 100<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5904\u7406 NaN<\/strong><\/h3>\n\n\n\n<p><code>NaN<\/code> \u662f JavaScript \u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u503c\uff0c\u8868\u793a\u201c\u975e\u6570\u503c\u201d\u6216\u9519\u8bef\u7684\u6570\u503c\u7ed3\u679c\u3002\u5b83\u5e76\u4e0d\u7b49\u4e8e\u4efb\u4f55\u503c\uff0c\u5305\u62ec\u5b83\u81ea\u5df1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1aNaN \u5224\u65ad<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>isNaN()<\/code> \u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u662f <code>NaN<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(isNaN(\"hello\"));   \/\/ true \uff08\"hello\" \u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57\uff09\nconsole.log(isNaN(123));       \/\/ false \uff08123 \u662f\u4e00\u4e2a\u6570\u5b57\uff09\nconsole.log(isNaN(NaN));       \/\/ true \uff08NaN \u662f NaN\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>Number \u7c7b\u578b\u7684\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>Number()<\/code> \u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>Number()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u4e3a\u6570\u5b57\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(Number(\"123\"));    \/\/ 123\nconsole.log(Number(\"123.45\")); \/\/ 123.45\nconsole.log(Number(\"hello\"));  \/\/ NaN\nconsole.log(Number(true));     \/\/ 1\nconsole.log(Number(false));    \/\/ 0<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>parseInt()<\/code> \u548c <code>parseFloat()<\/code><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>parseInt()<\/code>\uff1a\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6574\u6570\u3002<\/li>\n\n\n\n<li><code>parseFloat()<\/code>\uff1a\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6d6e\u52a8\u70b9\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(parseInt(\"123\"));    \/\/ 123\nconsole.log(parseInt(\"123.45\")); \/\/ 123\nconsole.log(parseFloat(\"123.45\")); \/\/ 123.45<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u6570\u5b57\u683c\u5f0f\u5316<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u7535\u5546\u5e73\u53f0\uff0c\u4f60\u9700\u8981\u5c06\u4ef7\u683c\u683c\u5f0f\u5316\u4e3a\u56fa\u5b9a\u7684\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u5e76\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u6765\u5c55\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function formatPrice(price) {\n  return price.toFixed(2);\n}\n\nconsole.log(formatPrice(10));      \/\/ \"10.00\"\nconsole.log(formatPrice(10.5));    \/\/ \"10.50\"\nconsole.log(formatPrice(123.456)); \/\/ \"123.46\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p><code>Number<\/code> \u5bf9\u8c61\u5728 JavaScript \u4e2d\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u7684\u8f6c\u6362\u3001\u683c\u5f0f\u5316\u548c\u6570\u5b66\u8ba1\u7b97\u3002\u901a\u8fc7\u5b83\u7684\u4e00\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u63a7\u5236\u6570\u5b57\u7684\u5c0f\u6570\u4f4d\u6570\uff0c\u5904\u7406\u4e0d\u540c\u8fdb\u5236\u7b49\u3002\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u7406\u89e3\u5e76\u719f\u7ec3\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4e8e\u6570\u636e\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Number \u5bf9\u8c61\u662f\u7528\u6765\u8868\u793a\u6570\u5b57\u503c\u7684\u5bf9\u8c61\u7c7b\u578b\u3002\u867d\u7136 JavaScript \u4e2d\u7684\u6570\u5b57\uff08\u65e0\u8bba [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2127","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2127"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2127\/revisions"}],"predecessor-version":[{"id":2128,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2127\/revisions\/2128"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}