{"id":9382,"date":"2015-03-30T20:20:49","date_gmt":"2015-03-30T20:20:49","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=9382"},"modified":"2015-03-30T20:20:49","modified_gmt":"2015-03-30T20:20:49","slug":"java-number-formatting-locale","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-number-formatting-locale\/","title":{"rendered":"Java number formatting:  Locale"},"content":{"rendered":"<h1>The tutor continues about number formatting with Java.<\/h1>\n<p>We don&#8217;t live in countries any more; rather,\u00a0 we live in the world.\u00a0 Right now, for instance, my family is off in the US.\u00a0 I&#8217;m not with them; I stayed home to run the business.\u00a0 In fact, I&#8217;m not a traveller.<\/p>\n<p>I learn about the world from print, which is how\u00a0 I recently became aware that number formats vary from country to country.\u00a0 Behold the many faces of 1 139 275.78:<\/p>\n<table>\n<tr>\n<td>France<\/td>\n<td>1 139 275,78<\/td>\n<\/tr>\n<tr>\n<td>Germany<\/td>\n<td>1.139.275,78<\/td>\n<\/tr>\n<tr>\n<td>US, Canada, English<\/td>\n<td>1,139,275.78<\/td>\n<\/tr>\n<tr>\n<td>Canada (French)<\/td>\n<td>1 139 275,78<\/td>\n<\/tr>\n<\/table>\n<p>Notice that none of those nation-specific formats is the way I write the number (once again: 1 139 275.78).  I was taught that, in Metric, the thousands separators are spaces, not commas.  Yet, being in an English-speaking country, I use a decimal point rather than a comma.  It&#8217;s an interesting mash of formats that I believe  scientifically-educated North Americans commonly use:)<\/p>\n<p>Being an international programming language, Java is prepared to accommodate the number formats used in different countries.  Java calls a country a <span style=\"font-variant:small-caps\">locale<\/span>.<\/p>\n<p>I wrote two earlier posts on Java decimal number formatting <a href=\"?p=9296\">here<\/a> and <a href=\"?p=9359\">here<\/a>.  Any Java programmer who is reading this article likely wants the key lines that enable a program to translate a number from one country&#8217;s format to another&#8217;s.  You start with an instance of NumberFormat, setting it with the desired locale:<br \/>\n<span style=\"font-family:monospace\"><br \/>\nNumberFormat numform = NumberFormat.getInstance(Locale.COUNTRY)<br \/>\n<\/span><br \/>\nFor example, to get the format used in France:<\/p>\n<p><span style=\"font-family:monospace\">NumberFormat numform0 = NumberFormat.getInstance(Locale.FRANCE);<\/span><\/p>\n<p>Next, you cast the NumberFormat instance into a DecimalFormat object:<\/p>\n<p><span style=\"font-family:monospace\">DecimalFormat decform0 = (DecimalFormat)numform0;<br \/>\n<\/span><\/p>\n<p>Finally, you apply the format descriptor string; for example,<\/p>\n<p><span style=\"font-family:monospace\">decform0.applyPattern(&#8220;#,##0.00&#8221;);<\/span><\/p>\n<p>Now, you can format number <span style=\"font-family:monospace\">num0<\/span> to String <span style=\"font-family:monospace\">string0<\/span>, in the style of France:<\/p>\n<p><span style=\"font-family:monospace\">string0 = decform0.format(num0);<\/span><\/p>\n<p>To use the above code effectively, you&#8217;ll need to include the lines<br \/>\n<span style=\"font-family:monospace\"><br \/>\nimport java.text.NumberFormat;<br \/>\nimport java.text.DecimalFormat;<br \/>\nimport java.util.Locale;<br \/>\n<\/span><\/p>\n<p>above the <span style=\"font-family:monospace\">class<\/span> definition.<\/p>\n<p>As far as I&#8217;ve read, not necessarily every country has a <span style=\"font-variant:small-caps\">locale<\/span> in Java.  You can see which ones do <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Locale.html\">here<\/a>.<\/p>\n<p>HTH:)<\/p>\n<p><em>Other sources:<\/em><\/p>\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/i18n\/format\/decimalFormat.html\">docs.oracle.com<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/13049791\/how-to-format-a-number-in-american-and-european-style-in-c\">stackoverflow.com<\/a><\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor continues about number formatting with Java. We don&#8217;t live in countries any more; rather,\u00a0 we live in the world.\u00a0 Right now, for instance, my family is off in the US.\u00a0 I&#8217;m not with them; I stayed home to &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-number-formatting-locale\/\"> <span class=\"screen-reader-text\">Java number formatting:  Locale<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[642,651,647,652,653,602],"class_list":["post-9382","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-java-decimalformat","tag-java-international-number-format","tag-java-number-format","tag-java-numberformat","tag-java-numberformat-locale","tag-java-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=9382"}],"version-history":[{"count":39,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9382\/revisions"}],"predecessor-version":[{"id":9421,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9382\/revisions\/9421"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=9382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=9382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=9382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}