{"id":2211,"date":"2023-01-07T11:22:33","date_gmt":"2023-01-07T03:22:33","guid":{"rendered":"https:\/\/cowmanchiang.me\/wp\/?p=2211"},"modified":"2023-10-31T15:41:35","modified_gmt":"2023-10-31T07:41:35","slug":"java-string-with-number-format","status":"publish","type":"post","link":"https:\/\/cowmanchiang.me\/wp\/?p=2211","title":{"rendered":"[Java] String with number format"},"content":{"rendered":"<p><a href=\"https:\/\/stackoverflow.com\/questions\/3672731\/how-can-i-format-a-string-number-to-have-commas-and-round\">Reference<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public static void main(String[] args) throws Exception {\n    String number = &quot;1000500000&quot;;\n    double amount = Double.parseDouble(number);\n    DecimalFormat intFormatter = new DecimalFormat(&quot;#,###&quot;);\n    DecimalFormat floatFormatter = new DecimalFormat(&quot;#,###.00&quot;);\n\n    System.out.println(&quot;==int==&quot;);\n    System.out.println(intFormatter.format(amount));\n    System.out.println(String.format(&quot;%,d&quot;, Integer.parseInt((number))));\n    System.out.println(number.replaceAll(&quot;(\\\\d)(?=(\\\\d{3})+$)&quot;, &quot;$1,&quot;));\n\n    System.out.println(&quot;==float==&quot;);\n    System.out.println(floatFormatter.format(amount));\n    System.out.println(String.format(&quot;%,.2f&quot;, Float.parseFloat((number))));\n}\n\n\n==int==\n1,000,500,000\n1,000,500,000\n1,000,500,000\n==float==\n1,000,500,000.00\n1,000,499,968.00<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reference public static void main(String[] args) throws Exception { String number = &quot;1000500000&quot;; double amount = Double.parseDouble(number); DecimalFormat intFormatter = new DecimalFormat(&quot;#,###&quot;); DecimalFormat floatFormatter = new DecimalFormat(&quot;#,###.00&quot;); System.out.println(&quot;==int==&quot;); System.out.println(intFormatter.format(amount)); System.out.println(String.format(&quot;%,d&quot;, Integer.parseInt((number)))); System.out.println(number.replaceAll(&quot;(\\\\d)(?=(\\\\d{3})+$)&quot;, &quot;$1,&quot;)); System.out.println(&quot;==float==&quot;); System.out.println(floatFormatter.format(amount)); System.out.println(String.format(&quot;%,.2f&quot;, Float.parseFloat((number)))); } ==int== 1,000,500,000 &hellip; <a href=\"https:\/\/cowmanchiang.me\/wp\/?p=2211\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-2211","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2211"}],"version-history":[{"count":6,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2217,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2211\/revisions\/2217"}],"wp:attachment":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}