{"id":1057522,"date":"2024-12-31T15:08:44","date_gmt":"2024-12-31T07:08:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057522.html"},"modified":"2024-12-31T15:08:47","modified_gmt":"2024-12-31T07:08:47","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86str%e8%bd%ac%e6%8d%a2%e4%b8%bafloat","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057522.html","title":{"rendered":"python\u5982\u4f55\u5c06str\u8f6c\u6362\u4e3afloat"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/fd9dd5af-1dca-48ce-bd97-6f03984f54b2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06str\u8f6c\u6362\u4e3afloat\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\uff08str\uff09\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08float\uff09\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570\u3001\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3001\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u60c5\u51b5\uff0c\u5982\u5904\u7406\u5305\u542b\u9017\u53f7\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u51fd\u6570float()\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ea\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9float()\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;123.45&quot;<\/p>\n<p>float_num = float(str_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5904\u7406\u5305\u542b\u9017\u53f7\u7684\u6570\u5b57\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u9017\u53f7\uff0c\u8fd9\u4f1a\u5bfc\u81f4float()\u51fd\u6570\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5148\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num_with_comma = &quot;1,234.56&quot;<\/p>\n<p>str_num_without_comma = str_num_with_comma.replace(&quot;,&quot;, &quot;&quot;)<\/p>\n<p>float_num = float(str_num_without_comma)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a1234.56<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e0d\u6b63\u786e\uff0cfloat()\u51fd\u6570\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;abc&quot;<\/p>\n<p>try:<\/p>\n<p>    float_num = float(str_num)<\/p>\n<p>    print(float_num)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6210\u529f\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5b9e\u73b0\u548c\u4f7f\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570float()\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9float()\u51fd\u6570\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;123.45&quot;<\/p>\n<p>float_num = float(str_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u7528\u4e8e\u683c\u5f0f\u6b63\u786e\u7684\u5b57\u7b26\u4e32\uff0c\u4e14\u8f6c\u6362\u901f\u5ea6\u5feb\u3002\u7136\u800c\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e0d\u6b63\u786e\uff0cfloat()\u51fd\u6570\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u8fd9\u79cd\u60c5\u51b5\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406\u5305\u542b\u9017\u53f7\u7684\u6570\u5b57\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u4f1a\u9047\u5230\u5305\u542b\u9017\u53f7\u7684\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528float()\u51fd\u6570\u4f1a\u5bfc\u81f4\u8f6c\u6362\u5931\u8d25\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num_with_comma = &quot;1,234.56&quot;<\/p>\n<p>str_num_without_comma = str_num_with_comma.replace(&quot;,&quot;, &quot;&quot;)<\/p>\n<p>float_num = float(str_num_without_comma)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a1234.56<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5305\u542b\u9017\u53f7\u7684\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u786e\u4fdd\u8f6c\u6362\u7684\u6210\u529f\u6027\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e\u53bb\u9664\u9017\u53f7\uff0c\u5bf9\u4e8e\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u8fd8\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e0d\u6b63\u786e\uff0cfloat()\u51fd\u6570\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;abc&quot;<\/p>\n<p>try:<\/p>\n<p>    float_num = float(str_num)<\/p>\n<p>    print(float_num)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u5373\u4f7f\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\u4e5f\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6355\u83b7\u5f02\u5e38\u540e\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u5982\u63d0\u793a\u7528\u6237\u8f93\u5165\u6b63\u786e\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5305\u542b\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u9017\u53f7\u5916\uff0c\u5b57\u7b26\u4e32\u4e2d\u8fd8\u53ef\u80fd\u5305\u542b\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u8d27\u5e01\u7b26\u53f7\u7b49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u53bb\u9664\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num_with_special_chars = &quot; $ 1,234.56 &quot;<\/p>\n<p>str_num_cleaned = str_num_with_special_chars.replace(&quot;$&quot;, &quot;&quot;).replace(&quot;,&quot;, &quot;&quot;).strip()<\/p>\n<p>float_num = float(str_num_cleaned)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a1234.56<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5305\u542b\u5404\u79cd\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u786e\u4fdd\u8f6c\u6362\u7684\u6210\u529f\u6027\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u53bb\u9664\u76f8\u5e94\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6279\u91cf\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6279\u91cf\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5c06\u5176\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u8fdb\u884c\u6279\u91cf\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;123.45&quot;, &quot;678.90&quot;, &quot;1,234.56&quot;]<\/p>\n<p>float_list = [float(num.replace(&quot;,&quot;, &quot;&quot;)) for num in str_list]<\/p>\n<p>print(float_list)  # \u8f93\u51fa\uff1a[123.45, 678.9, 1234.56]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6279\u91cf\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u8003\u8651\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u60c5\u51b5\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7684\u903b\u8f91\u5c01\u88c5\u5728\u81ea\u5b9a\u4e49\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_float(str_num):<\/p>\n<p>    try:<\/p>\n<p>        str_num_cleaned = str_num.replace(&quot;,&quot;, &quot;&quot;).strip()<\/p>\n<p>        return float(str_num_cleaned)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>str_list = [&quot;123.45&quot;, &quot;678.90&quot;, &quot;1,234.56&quot;, &quot;abc&quot;]<\/p>\n<p>float_list = [str_to_float(num) for num in str_list]<\/p>\n<p>print(float_list)  # \u8f93\u51fa\uff1a[123.45, 678.9, 1234.56, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5904\u7406\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u53ef\u80fd\u91c7\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u3002\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528float()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u56e0\u4e3aPython\u7684float()\u51fd\u6570\u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num_sci = &quot;1.23e4&quot;<\/p>\n<p>float_num = float(str_num_sci)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a12300.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u786e\u4fdd\u8f6c\u6362\u7684\u6210\u529f\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>str_complex = &quot;The price is $1,234.56&quot;<\/p>\n<p>match = re.search(r&quot;[-+]?\\d*\\.\\d+|\\d+&quot;, str_complex)<\/p>\n<p>if match:<\/p>\n<p>    float_num = float(match.group().replace(&quot;,&quot;, &quot;&quot;))<\/p>\n<p>    print(float_num)  # \u8f93\u51fa\uff1a1234.56<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No valid number found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u63d0\u53d6\u5e76\u8f6c\u6362\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5305\u542b\u8d1f\u53f7\u548c\u6b63\u53f7\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u53ef\u80fd\u5305\u542b\u8d1f\u53f7\u548c\u6b63\u53f7\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528float()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u4f46\u9700\u8981\u786e\u4fdd\u8d1f\u53f7\u548c\u6b63\u53f7\u7684\u4f4d\u7f6e\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num_negative = &quot;-123.45&quot;<\/p>\n<p>str_num_positive = &quot;+123.45&quot;<\/p>\n<p>float_num_negative = float(str_num_negative)<\/p>\n<p>float_num_positive = float(str_num_positive)<\/p>\n<p>print(float_num_negative)  # \u8f93\u51fa\uff1a-123.45<\/p>\n<p>print(float_num_positive)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5305\u542b\u8d1f\u53f7\u548c\u6b63\u53f7\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u786e\u4fdd\u8f6c\u6362\u7684\u6210\u529f\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570\u3001\u5904\u7406\u5305\u542b\u9017\u53f7\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3001\u6279\u91cf\u5904\u7406\u5b57\u7b26\u4e32\u5217\u8868\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57\u3001\u5904\u7406\u5305\u542b\u8d1f\u53f7\u548c\u6b63\u53f7\u7684\u5b57\u7b26\u4e32\u7b49\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6210\u529f\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u6b63\u786e\uff0c\u4f8b\u5982\u5305\u542b\u6709\u6548\u7684\u6570\u5b57\u548c\u5c0f\u6570\u70b9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">num_str = &quot;3.14&quot;\nnum_float = float(num_str)\nprint(num_float)  # \u8f93\u51fa\uff1a3.14\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u5c06\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u65f6\uff0c\u5c06\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">num_str = &quot;3.14abc&quot;\ntry:\n    num_float = float(num_str)\nexcept ValueError:\n    print(&quot;\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u8bf7\u68c0\u67e5\u5b57\u7b26\u4e32\u683c\u5f0f\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u79d1\u5b66\u8ba1\u6570\u6cd5\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>float()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">num_str = &quot;1.5e2&quot;  # \u8868\u793a150.0\nnum_float = float(num_str)\nprint(num_float)  # \u8f93\u51fa\uff1a150.0\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5904\u7406\u5927\u6570\u6216\u5c0f\u6570\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\uff08str\uff09\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff08float\uff09\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684float()\u51fd\u6570\u3001\u5904 [&hellip;]","protected":false},"author":3,"featured_media":1057529,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057522"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1057522"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057522\/revisions"}],"predecessor-version":[{"id":1057531,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057522\/revisions\/1057531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057529"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}