{"id":7340,"date":"2014-12-27T04:34:23","date_gmt":"2014-12-27T04:34:23","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=7340"},"modified":"2014-12-27T04:37:27","modified_gmt":"2014-12-27T04:37:27","slug":"perl-decimal-formatting-using-printf-and-sprintf","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-decimal-formatting-using-printf-and-sprintf\/","title":{"rendered":"Perl:  decimal formatting using printf and sprintf"},"content":{"rendered":"<h1>Tutoring math, a likely destination of some of your students is computer science. The tutor continues coverage of the Perl language, which seems flush with simple solutions to programming problems&#8230;.<\/h1>\n<p>&nbsp;<br \/>\nIn yesterday&#8217;s post, which was about Perl subroutines, I introduced an example program that calculates the final price of an item from its sticker price, the discount, and the tax rate.<\/p>\n<p>In my context, anyway, the program works great &#8211; but any zealots who typed and ran it on Boxing Day noticed that it does not format the answers as a cash register would.  For example, if you give it the inputs<\/p>\n<p>price:       78<br \/>\ndiscount:    33<br \/>\ntax rate:    12<\/p>\n<p>it will give back<\/p>\n<p>final price:  58.5312<\/p>\n<p>Prices are given to two decimal places, not four.  How can the program be fixed to give answers formatted as from a cash register?<\/p>\n<p>Perl has two answers to this problem:  printf(<em>format<\/em>, <em>number<\/em>) and sprintf(<em>format<\/em>, <em>number<\/em>).  Today, we&#8217;ll look at a few lines of code that show the use of these built-in functions:<\/p>\n<p><span style=\"color:brown;font-family:monospace\"><br \/>\n#!\/usr\/bin\/perl<\/br><br \/>\nprint &#8220;Enter a number, please.&#8221;;<br \/>\nprint &#8220;This program will round your number to two decimal places.&#8221;;<br \/>\n$num=&lt;STDIN&gt;;<br \/>\n$rounded=sprintf(&#8220;%.2f&#8221;,$num);<br \/>\nprint &#8220;Using printf, your number, rounded to two decimal places, is &#8220;;<br \/>\nprintf(&#8220;%.2f&#8221;,$num);<br \/>\nprint &#8220;\\n\\nRecall:  the number you entered is $num&#8221;;<br \/>\nprint &#8220;\\n\\nFrom sprintf, rounded to two decimal places, &#8220;;<br \/>\nprint &#8220;your number is $rounded&#8221;;<\/p>\n<p>Let&#8217;s compare sprintf and printf.  Both leave the input number unchanged.  The function sprintf reformats it, and saves the reformatted number in a new variable.  The function printf simply prints the number in the specified format.<\/p>\n<p>I wish all of you the best of nights:)<\/p>\n<p>Source:<\/p>\n<p>McGrath, Mike.  <em>Perl in easy steps<\/em>.  Southam:  Computer Step, 2004.<\/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>Tutoring math, a likely destination of some of your students is computer science. The tutor continues coverage of the Perl language, which seems flush with simple solutions to programming problems&#8230;. &nbsp; In yesterday&#8217;s post, which was about Perl subroutines, I &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-decimal-formatting-using-printf-and-sprintf\/\"> <span class=\"screen-reader-text\">Perl:  decimal formatting using printf and sprintf<\/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":[378,173,379,380],"class_list":["post-7340","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-decimal-formatting","tag-perl-programming","tag-printf","tag-sprintf"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7340","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=7340"}],"version-history":[{"count":23,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7340\/revisions"}],"predecessor-version":[{"id":7363,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7340\/revisions\/7363"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=7340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=7340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=7340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}