{"id":57747,"date":"2023-10-20T20:42:20","date_gmt":"2023-10-20T12:42:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=57747"},"modified":"2023-10-20T20:42:22","modified_gmt":"2023-10-20T12:42:22","slug":"java%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/57747.html","title":{"rendered":"Java\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u200b\u638c\u63e1Java\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6570\u5b57\u7684\u6280\u5de7\u662f\u7a0b\u5e8f\u5458\u7684\u57fa\u672c\u9700\u6c42\u3002\u6b64\u6587\u5c06\u5206\u6b65\u5f15\u5bfc\u4f60\u7406\u89e3\uff1a1.\u4f7f\u7528<strong>Integer.parseInt()<\/strong>\u548c<strong>Double.parseDouble()<\/strong>\u7684\u57fa\u7840\u8f6c\u6362\u65b9\u6cd5\uff1b2.\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff1b3.<strong>NumberFormat<\/strong>\u7684\u5e94\u7528\uff1b4.\u4f7f\u7528Java 8\u7684<strong>Optional<\/strong>\u5904\u7406\u7a7a\u503c\uff1b5.\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u95f4\u7684\u5173\u7cfb\u548c\u91cd\u8981\u6027\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u660e\u786e\u5f85\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1\uff0e\u4f7f\u7528Integer.parseInt()\u548cDouble.parseDouble()\u7684\u57fa\u7840\u8f6c\u6362\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u5f53\u6211\u4eec\u60f3\u4ece\u4e00\u4e2a\u7eaf\u6570\u5b57\u5b57\u7b26\u4e32\u4e2d\u83b7\u53d6\u6574\u6570\u6216\u5c0f\u6570\u65f6\uff0cJava\u63d0\u4f9b\u4e86\u975e\u5e38\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String numStr = \"1234\"; int number = Integer.parseInt(numStr);<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u6d6e\u70b9\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String floatStr = \"123.45\"; double floatingNumber = Double.parseDouble(floatStr);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38<\/h2>\n\n\n\n<p>\u4e0d\u662f\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u53ef\u4ee5\u8f7b\u6613\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u542b\u6709\u975e\u6570\u5b57\u5b57\u7b26\u6216\u4e3a\u7a7a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u4f1a\u629b\u51fa<strong>NumberFormatException<\/strong>\u3002\u4e3a\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u8fd9\u79cd\u5f02\u5e38\uff1a<\/p>\n\n\n\n<p>try { int number = Integer.parseInt(numStr); } catch(NumberFormatException e) { System.out.println(&#8220;\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57.&#8221;); }<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4f7f\u7528NumberFormat\u7684\u5e94\u7528<\/h2>\n\n\n\n<p>\u5f53\u4f60\u5904\u7406\u56fd\u9645\u5316\u5e94\u7528\u65f6\uff0c\u4e0d\u540c\u7684\u533a\u57df\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u5b57\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u56fd\u5bb6\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5c0f\u6570\u70b9\u3002<strong>NumberFormat<\/strong>\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NumberFormat format = NumberFormat.getInstance(Locale.FRANCE); Number number = format.parse(\"1,234\");<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u4f7f\u7528Java 8\u7684Optional\u5904\u7406\u7a7a\u503c<\/h2>\n\n\n\n<p>\u5728\u5c1d\u8bd5\u8f6c\u6362\u4e4b\u524d\uff0c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u6216null\u662f\u5f88\u91cd\u8981\u7684\u3002Java 8\u5f15\u5165\u7684<strong>Optional<\/strong>\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u53ef\u80fd\u7684null\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Optional&lt;String&gt; optionalNumStr = Optional.ofNullable(numStr); if(optionalNumStr.isPresent()) { \/\/ \u8f6c\u6362\u903b\u8f91 }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u95f4\u7684\u5173\u7cfb\u548c\u91cd\u8981\u6027<\/h2>\n\n\n\n<p>\u5728\u73b0\u5b9e\u4e16\u754c\u7684\u5e94\u7528\u4e2d\uff0c\u4ece\u7528\u6237\u8f93\u5165\u3001\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u6765\u6e90\u8bfb\u53d6\u7684\u6570\u636e\u901a\u5e38\u90fd\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u4e3a\u4e86\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u6216\u6570\u636e\u6bd4\u8f83\uff0c\u8f6c\u6362\u6210\u6570\u5b57\u683c\u5f0f\u662f\u5fc5\u8981\u7684\u3002\u7136\u800c\uff0c\u8fd9\u4e00\u8f6c\u6362\u5e76\u4e0d\u603b\u662f\u5e73\u6ed1\u7684\u3002\u975e\u6cd5\u7684\u5b57\u7b26\u4e32\u3001\u610f\u5916\u7684\u5b57\u7b26\u3001\u533a\u57df\u8bbe\u7f6e\u7b49\u90fd\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u4f5c\u4e3a\u5f00\u53d1\u8005\uff0c\u6211\u4eec\u4e0d\u4ec5\u8981\u77e5\u9053\u5982\u4f55\u8f6c\u6362\uff0c\u8fd8\u8981\u7406\u89e3\u4f55\u65f6\u548c\u4e3a\u4ec0\u4e48\u9700\u8981\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u7ed3\u8bba\uff1a Java\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u3002\u4f46\u5173\u952e\u5728\u4e8e\uff0c\u6211\u4eec\u9700\u8981\u65f6\u523b\u8b66\u60d5\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u63aa\u65bd\u3002\u53ea\u6709\u8fd9\u6837\uff0c\u6211\u4eec\u624d\u80fd\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5065\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1280\" height=\"850\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u5982\u4f55\u4e0b\u8f7dgithub\u7684\u6587\u4ef6-89.jpg?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-57748\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u5982\u4f55\u4e0b\u8f7dgithub\u7684\u6587\u4ef6-89.jpg 1280w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u5982\u4f55\u4e0b\u8f7dgithub\u7684\u6587\u4ef6-89-768x510.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/figure>\n\n\n\n<p><strong>\u5e38\u89c1\u95ee\u7b54\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5728\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u65f6\u6211\u4f1a\u5f97\u5230NumberFormatException\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a\u5f53\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u4e0d\u4ee3\u8868\u6709\u6548\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff08\u4f8b\u5982\u201cabc\u201d\u6216\u201c12.34.56\u201d\uff09\u8f6c\u6362\u4e3a\u6570\u5b57\u65f6\uff0cJava\u4f1a\u629b\u51fa<strong>NumberFormatException<\/strong>\u3002\u786e\u4fdd\u4f60\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u4ec5\u5305\u542b\u6709\u6548\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u4e14\u683c\u5f0f\u6b63\u786e\u3002\u4f8b\u5982\uff0c\u6574\u6570\u4e0d\u5e94\u8be5\u5305\u542b\u5c0f\u6570\u70b9\uff0c\u800c\u6d6e\u70b9\u6570\u5219\u5e94\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5c0f\u6570\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u95ee\uff1a\u5728Java\u4e2d\uff0c\u6211\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u5b57\u7c7b\u578b\uff0c\u5982float\u6216double\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5305\u88c5\u7c7b\u7684<strong>parse<\/strong>\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<strong>float<\/strong>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<strong>Float.parseFloat(yourString)<\/strong>\u3002\u5bf9\u4e8e<strong>double<\/strong>\u7c7b\u578b\uff0c\u4f7f\u7528<strong>Double.parseDouble(yourString)<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u95ee\uff1a\u9664\u4e86parseInt\u548cvalueOf\uff0c\u8fd8\u6709\u5176\u4ed6\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u65b9\u6cd5\u5417\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a\u662f\u7684\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<strong>java.text.NumberFormat<\/strong>\u7c7b\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6570\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u56fd\u9645\u5316\u7684\u6570\u5b57\u683c\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u89e3\u6790\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5c0f\u6570\u70b9\u7684\u6570\u5b57\u6216\u5176\u4ed6\u5730\u533a\u7279\u5b9a\u7684\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u95ee\uff1a\u4e3a\u4ec0\u4e48\u6211\u5728\u5c06\u5b57\u7b26\u4e32\u201c0123\u201d\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u5f97\u5230123\uff0c\u524d\u9762\u76840\u88ab\u53bb\u6389\u4e86\uff1f<\/li>\n\n\n\n<li>\u7b54\uff1a\u5f53\u4f60\u4f7f\u7528<strong>Integer.parseInt<\/strong>\u6216<strong>Integer.valueOf<\/strong>\u65b9\u6cd5\u5c06\u5e26\u6709\u524d\u5bfc\u96f6\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0cJava\u4f1a\u5ffd\u7565\u524d\u5bfc\u96f6\uff0c\u56e0\u4e3a\u5728\u6570\u5b66\u4e2d\uff0c\u524d\u5bfc\u96f6\u4e0d\u4f1a\u6539\u53d8\u4e00\u4e2a\u6570\u5b57\u7684\u503c\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u200b\u638c\u63e1Java\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6570\u5b57\u7684\u6280\u5de7\u662f\u7a0b\u5e8f\u5458\u7684\u57fa\u672c\u9700\u6c42\u3002\u6b64\u6587\u5c06\u5206\u6b65\u5f15\u5bfc\u4f60\u7406\u89e3\uff1a1.\u4f7f\u7528Integer.par [&hellip;]","protected":false},"author":7,"featured_media":57749,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[367],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/57747"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=57747"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/57747\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/57749"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=57747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=57747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=57747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}