{"id":517,"date":"2022-04-02T16:34:34","date_gmt":"2022-04-02T08:34:34","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=517"},"modified":"2022-07-27T16:35:25","modified_gmt":"2022-07-27T08:35:25","slug":"lua%e4%b8%adtonumber%e5%81%9a%e4%ba%86%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=517","title":{"rendered":"lua\u4e2dtonumber\u505a\u4e86\u4ec0\u4e48"},"content":{"rendered":"<h1>1. \u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50<\/h1>\n<pre><code class=\"line-numbers\">    a = tonumber(\"2333\")\n\n<\/code><\/pre>\n<h1>\u56e0\u4e3atonumber\u662flua\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570,\u6240\u4ee5\u6211\u4eec\u6765\u770b\u5bf9\u5e94\u7684\u51fd\u6570<\/h1>\n<pre><code class=\"line-numbers\">static int gafqBase_tonumber(gafq_State *L)\n{\n    int base = gafqL_optint(L, 2, 10); \/\/ \u83b7\u53d6\u7b2c\u4e8c\u4e2a\u53c2\u6570,\u8fdb\u5236 \u9ed8\u8ba410 ,\u53ef\u4ee5\u662f2-36\u7684\u503c\n    if (base == 10)\n    {\n        gafqL_checkany(L, 1); \/\/ \u68c0\u67e5\u7b2c\u4e00\u4e2a\u53c2\u6570,\u4e0d\u662f\u7a7a\n        if (gafq_isnumber(L, 1)) \/\/ \u5224\u65ad\u662f\u5426\u662f\u6570\u5b57\n        {\n            gafq_pushnumber(L, gafq_tonumber(L, 1)); \/\/ \u8f6c\u6210\u6570\u5b57\n            return 1;\n        }\n    }\n    else\n    {\n        const char *s1 = gafqL_checkstring(L, 1);\n        char *s2;\n        unsigned long n;\n        gafqL_argcheck(L, 2 &lt;= base &amp;&amp; base &lt;= 36, 2, \"base out of range\");\n        \/\/ \u8c03\u7528\u5e93\u51fd\u6570strtoul   \n        \/\/ C \u5e93\u51fd\u6570unsigned long int strtoul(const char *str, char **endptr, int base)\u51fd\u6570\u6839\u636e\u7ed9\u5b9a\u7684base\u5c06str\u4e2d\u5b57\u7b26\u4e32\u7684\u521d\u59cb\u90e8\u5206\u8f6c\u6362\u4e3aunsigned long int \u503c\uff0c\n        \/\/ \u8be5\u503c\u5fc5\u987b\u4ecb\u4e8e 2 \u548c36\uff08\u542b\uff09\uff0c\u6216\u4e3a\u7279\u6b8a\u503c 0\u3002\n        n = strtoul(s1, &amp;s2, base);\n        if (s1 != s2)\n        {\n            while (isspace((unsigned char)(*s2)))\n                s2++;\/\/ \u53bb\u6389\u6240\u6709\u7a7a\u683c\n            if (*s2 == '\\0')\n            {   \/\/ \u662f\u6b63\u786e\u7684\u503c\n                gafq_pushnumber(L, (gafq_Number)n);\n                return 1;\n            }\n        }\n    }\n    gafq_pushnil(L);\/\/ \u8f6c\u6362\u5931\u8d25 \u653e\u5165nil\n    return 1;\n}\n<\/code><\/pre>\n<h1>gafq_isnumber\u505a\u4e86\u4ec0\u4e48<\/h1>\n<pre><code class=\"line-numbers\">\/\/\u5224\u65ad\u6574\u6570\nGAFQ_API int gafq_isnumber(gafq_State *L, int idx)\n{\n    TValue n;\n    const TValue *o = idx_to_Tvalue(L, idx); \/\/ \u53d6\u51fa\u5bf9\u5e94\u8981\u8f6c\u6362\u7684\u503c\n    return tonumber(o, &amp;n);\n}\n\n<\/code><\/pre>\n<h1>\u8df3\u8fc7\u4e0a\u9762\u73b0\u5728\u5148\u770bgafq_tonumber \u505a\u4e86\u4ec0\u4e48<\/h1>\n<pre><code class=\"line-numbers\">\/\/\u8f6c\u6210\u6570\u5b57\nGAFQ_API gafq_Number gafq_tonumber(gafq_State *L, int idx)\n{\n    TValue n;\n    const TValue *o = idx_to_Tvalue(L, idx);\n    if (tonumber(o, &amp;n))\n        return nvalue(o);\n    else\n        return 0;\n}\n<\/code><\/pre>\n<h1>\u6211\u4eec\u770b\u5230\u4e86gafq_isnumber\u548cgafq_tonumber\u90fd\u505a\u4e86\u540c\u6837\u7684\u4e8b<\/h1>\n<pre><code class=\"line-numbers\">    TValue n;\n    const TValue *o = idx_to_Tvalue(L, idx);\n    tonumber(o, &amp;n)\n<\/code><\/pre>\n<h1>tonumber\u662f\u4e00\u4e2a\u5b8f<\/h1>\n<pre><code class=\"line-numbers\">#define tonumber(o, n) (ttype(o) == GAFQ_TNUMBER || (((o) = gafqV_tonumber(o, n)) != NULL))\n<\/code><\/pre>\n<h1>\u90a3\u4e48\u6211\u4eec\u6765\u770b gafqV_tonumber \u505a\u4e86\u4ec0\u4e48<\/h1>\n<p>gafqV_tonumber \u5148\u5224\u65ad\u662f\u4e0d\u662f\u5df2\u7ecf\u662f\u6570\u5b57,\u5982\u679c\u662f\u6570\u5b57\u5c31\u4e0d\u7528\u8f6c\u6362\u4e86,\u5982\u679c\u662f\u5b57\u7b26\u4e32\u5219\u8fd8\u8981\u8f6c\u6362<\/p>\n<pre><code class=\"line-numbers\">const TValue *gafqV_tonumber(const TValue *obj, TValue *n)\n{\n    gafq_Number num;\n    if (ttisnumber(obj))\n        return obj;\n    if (ttisstring(obj) &amp;&amp; gafqO_str2d(svalue(obj), &amp;num))\n    {\n        setnvalue(n, num);\n        return n;\n    }\n    else\n        return NULL;\n}\n<\/code><\/pre>\n<h1>\u6211\u4eec\u6765\u770bgafqO_str2d\u505a\u4e86\u4ec0\u4e48<\/h1>\n<pre><code class=\"line-numbers\">\/\/ \u5b57\u7b26\u4e32\u8f6c\u6574\u6570\nint gafqO_str2d(const char *s, gafq_Number *result)\n{\n    char *endptr;\n    *result = gafq_str2number(s, &amp;endptr);\n    if (endptr == s)\n        return 0; \/\/ \u8f6c\u6362\u5931\u8d25\n    if (*endptr == 'x' || *endptr == 'X') \/\/ \u53ef\u80fd\u662f\u5341\u516d\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\n        *result = cast_num(strtoul(s, &amp;endptr, 16));\n    if (*endptr == '\\0')\n        return 1; \/\/ \u8f6c\u6362\u6210\u529f\n    while (isspace(cast(unsigned char, *endptr))) \/\/ \u53bb\u6389\u6240\u6709\u7a7a\u683c\n        endptr++;\n    if (*endptr != '\\0')\n        return 0; \/\/ \u53bb\u6389\u540e\u7eed\u7a7a\u683c\u8fd8\u662f\u8f6c\u6362\u5931\u8d25\n    return 1; \/\/ \u8f6c\u6362\u6210\u529f\n}\n\n<\/code><\/pre>\n<h1>gafq_str2number\u53c8\u662f\u4ec0\u4e48\u5462?<\/h1>\n<p>\u4ed6\u662f\u4e00\u4e2ac\u51fd\u6570strtod \u7684\u5b8f<\/p>\n<pre><code class=\"line-numbers\">    #define gafq_str2number(s, p) strtod((s), (p))\n<\/code><\/pre>\n<h1>\u7ed3\u8bba<\/h1>\n<p>tonumber\u4f1a\u5148\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e0d\u662f10, \u5982\u679c\u662f10,\u4ed6\u4f1a\u68c0\u67e5\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0d\u662f\u6570\u5b57,\u5982\u679c\u662f\u6570\u5b57,\u5c31\u4f1a\u8fdb\u884c\u8f6c\u5316.<br \/>\n\u5224\u65ad \u662f\u5426\u662f\u6570\u5b57 \u548c \u8f6c\u6362\u6210\u6570\u5b57,\u505a\u4e86\u76f8\u540c\u7684\u64cd\u4f5c<br \/>\n\u8f6c\u6362\u7684\u8fc7\u7a0b \u4f1a\u5148\u8c03\u7528\u5e93\u51fd\u6570 strtod<br \/>\n\u6839\u636e strtod \u7684\u6267\u884c\u7ed3\u679c\u5224\u65ad, \u5982\u679c\u6709x \u6216\u8005X\u4f1a\u989d\u5916\u5c1d\u8bd5\u8f6c\u6210\u621016\u8fdb\u5236<br \/>\n\u5982\u679c strtod \u7684\u7ed3\u679c\u53bb\u6389\u7a7a\u683c\u8fd8\u4e0d\u662f \\0 \u5c31\u4f1a\u8ba4\u4e3a\u8f6c\u6362\u5931\u8d25<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50 a = tonumber(&#8220;2333&#8221;) \u56e0\u4e3atonumber\u662flua\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570,\u6240<a href=\"https:\/\/www.ccagml.com\/?p=517\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">lua\u4e2dtonumber\u505a\u4e86\u4ec0\u4e48<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/517"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=517"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":518,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions\/518"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}