{"id":9296,"date":"2015-03-28T21:38:25","date_gmt":"2015-03-28T21:38:25","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=9296"},"modified":"2018-02-25T18:09:06","modified_gmt":"2018-02-25T18:09:06","slug":"java-formatting-decimal-numbers-part-0","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-formatting-decimal-numbers-part-0\/","title":{"rendered":"Java:  formatting decimal numbers, part 0"},"content":{"rendered":"<h1>We&#8217;re getting into a serious topic. The tutor rolls up his sleeves.<\/h1>\n<p>In math, formatting rarely matters. \u00a0If the correct answer is 0.012, you can answer as 1.2e-2, 3\/250, or 0.01200.  Most computer environments, like most calculators, give an &#8220;unformatted&#8221; answer to a calculation.  Not too surprisingly, when I ask Java for the square root of 14, it gives 3.7416573867739413, which is perhaps more than sufficient.<\/p>\n<p>In physics, chemistry, or financial contexts, people aren&#8217;t as tolerant of unformatted answers.  Scientists expect answers to correct precision.  Monetary values are expected to two decimal places.<\/p>\n<p>Java has a class that helps the programmer to display numeric output according to any desired format: DecimalFormat.  It&#8217;s invoked with a pattern that describes the desired format.  An example: <\/p>\n<p><span style=\"font-family:monospace;color:purple\">DecimalFormat form0 = new DecimalFormat(&#8220;#,###.00&#8221;);<\/span><\/p>\n<p>In order to use the DecimalFormat class, you must include the line<\/p>\n<p><span style=\"font-family:monospace;color:purple\">import java.text.DecimalFormat;<\/span><\/p>\n<p>above the class declaration.<\/p>\n<p>Following is a short program that illustrates the use of Java&#8217;s DecimalFormat. (It uses the Java Scanner, about which I wrote a series starting <a href=\"?p=9074\">here<\/a>.)<\/p>\n<p><span style=\"font-family:monospace;color:purple\"><br \/>\nimport java.text.DecimalFormat;<br \/>\nimport java.util.Scanner;<span style=\"color:orange\">\/\/needed for the scanner<\/span><br \/>\n<br \/>\nclass DecFormatEx{<br \/>\n<span style=\"color:orange\">\/*This class reads numbers given with the command call, then displays them formatted to two decimal places with the thousands separated by commas.*\/<\/span><br \/>\npublic static void main(String[] args){<br \/>\n<br \/>\nDecimalFormat dec0=new DecimalFormat(&#8220;#,###.00&#8221;);<br \/>\ndouble num0;<br \/>\nString string0;<br \/>\nfor(int i=0;i<args.length;i++){<br \/>\nScanner scan0=new Scanner(args[i]);<br \/>\nif(scan0.hasNextDouble()){<br \/>\nnum0=scan0.nextDouble();<br \/>\nstring0=dec0.format(num0);<br \/>\nSystem.out.println(&#8220;The number, formatted, is &#8220;+string0+&#8221;\\n&#8221;);<br \/>\n}<br \/>\nelse{<br \/>\nSystem.out.println(args[i] + &#8221; not a readable number.\\n&#8221;);<br \/>\n}<br \/>\n}\/\/<span style=\"color:orange\">end of for loop<\/span><br \/>\nSystem.out.println(&#8220;\\nSee you again:)\\n\\n&#8221;);<br \/>\n}\/\/<span style=\"color:orange\">end of main<\/span><br \/>\n}\/\/<span style=\"color:orange\">end of class<\/span><\/p>\n<p>Let&#8217;s imagine you save the above program as <span style=\"font-family:monospace\">DecFormatEx.java<\/span>, then compile it as follows:<\/p>\n<p><span style=\"font-family:monospace\">javac DecFormatEx.java<\/span><\/p>\n<p>Finally, you run it with the following command call:<\/p>\n<p><span style=\"font-family:monospace\">java DecFormatEx 1139.726 23 0.113e1 2000000.29<\/span><\/p>\n<p>You&#8217;ll hopefully receive the output<br \/>\n<span style=\"font-family:monospace\"><br \/>\nThe number, formatted, is 1,139.73<br \/>\nThe number, formatted, is 23.00<br \/>\nThe number, formatted, is 1.13<br \/>\nThe number, formatted, is 2,000,000.29<br \/>\n<\/span><\/p>\n<p>As usual, this little Java program needs more explanation than fits in one post.  I&#8217;ll be continuing about it tomorrow.<\/p>\n<p>HTH:)<\/p>\n<p>Source:<\/p>\n<p><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/text\/DecimalFormat.html\">docs.oracle.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>We&#8217;re getting into a serious topic. The tutor rolls up his sleeves. In math, formatting rarely matters. \u00a0If the correct answer is 0.012, you can answer as 1.2e-2, 3\/250, or 0.01200. Most computer environments, like most calculators, give an &#8220;unformatted&#8221; &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-formatting-decimal-numbers-part-0\/\"> <span class=\"screen-reader-text\">Java:  formatting decimal numbers, part 0<\/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":[643,644,645,646,642,647,602],"class_list":["post-9296","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-formatting-decimal-numbers-in-java","tag-formatting-money-values-in-java","tag-formatting-number-output-in-java","tag-formatting-numbers-java","tag-java-decimalformat","tag-java-number-format","tag-java-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9296","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=9296"}],"version-history":[{"count":64,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9296\/revisions"}],"predecessor-version":[{"id":30492,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9296\/revisions\/30492"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=9296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=9296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=9296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}