{"id":4671,"date":"2021-03-27T00:01:55","date_gmt":"2021-03-27T07:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4671"},"modified":"2021-04-03T08:31:35","modified_gmt":"2021-04-03T15:31:35","slug":"options-for-the-strfmon-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4671","title":{"rendered":"Options for the <em>strfmon()<\/em> Function"},"content":{"rendered":"<p>The <em>strfmon()<\/em> function, introduced in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4664\">last week&#8217;s Lesson<\/a> properly formats a monetary value for specific regions. To unlock the function&#8217;s various features, you must understand and use formatting placeholders.<br \/>\n<!--more--><br \/>\nThe placeholders used in the <em>strfmon()<\/em> function operate like placeholders used by <em>printf()<\/em> and <em>scanf()<\/em>, though different placeholders are used. Like the <em>printf()<\/em> function&#8217;s placeholders, the <em>strfmon()<\/em> placeholders are prefixed by the <code>%<\/code> character, which can be followed by optional characters to set specifics, then a final character to define the placeholder.<\/p>\n<p>The two common, and most reliable, placeholders are <code>%n<\/code> and <code>%i<\/code>. These set the output for the national and international currencies, respectively:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_03_27-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;locale.h&gt;\r\n#include &lt;monetary.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[32];\r\n    char *r;\r\n\r\n    r = setlocale(LC_ALL,\"en_US.UTF-8\");\r\n    if( r==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to set locale\\n\");\r\n        return(1);\r\n    }\r\n\r\n    strfmon(buffer,32,\"International format: %i\",359246.80);\r\n    puts(buffer);\r\n    strfmon(buffer,32,\"National format: %n\",359246.80);\r\n    puts(buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<p><code>International format: USD 359,246.80<br \/>\nNational format: $359,246.80<\/code><\/p>\n<p>As an aside, under OS X the International format was output as <code>USD359,24z???<\/code>, which is some kinda formatting bug on the Mac. If you&#8217;re using a Mac, be aware that the <em>strfmon()<\/em> function may not be properly implemented.<\/p>\n<p>In fact, be aware that this function may not be properly implemented on any platform. This reason may be why the function isn&#8217;t included with the MinGW compiler or why it&#8217;s relatively unknown.<\/p>\n<p>To explore the conversion character options, I concocted a test program. Like my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=288\">series on <em>printf()<\/em> conversion characters (placeholders)<\/a>, I figured a program that runs through all the options and variations would be keen, both to present how the options are used and their effect on output.<\/p>\n<p>Here&#8217;s how far I got:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_27-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_03_27-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;locale.h&gt;\r\n#include &lt;monetary.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[32];\r\n    char *r;\r\n\r\n    r = setlocale(LC_ALL,\"en_US.UTF-8\");\r\n    if( r==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to set locale\\n\");\r\n        return(1);\r\n    }\r\n\r\n    strfmon(buffer,32,\"Normal: %n\",359246.80);\r\n    puts(buffer);\r\n    strfmon(buffer,32,\"No grouping characters: %^n\",359246.80);\r\n    puts(buffer);\r\n    strfmon(buffer,32,\"No currency symbol: %!n\",359246.80);\r\n    puts(buffer);\r\n    strfmon(buffer,32,\"Left justification: %-30n\",359246.80);\r\n    puts(buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The three placeholder options I explored are:<\/p>\n<p>Line 19, The <code>^<\/code> character that suppresses the hundreds\/thousands separator.<\/p>\n<p>Line 21, the <code>!<\/code> character that suppresses the currency symbol.<\/p>\n<p>Line 23, The <code>-30<\/code> option that should set output width to 30 characters, left-justified.<\/p>\n<p>Here is the output generated on the Macintosh:<\/p>\n<p><code>Normal: $359,246.80<br \/>\nNo grouping characters: $359246:??<br \/>\nNo currency symbol: 359,246.80<br \/>\nLeft justification: 359,246.80<\/code><\/p>\n<p>And for Ubuntu Linux:<\/p>\n<p><code>Normal: $359,246.80<br \/>\nNo grouping characters: $359246:?<br \/>\nNo currency symbol: 359,246.80<br \/>\nLeft justification: 359,246.80<\/code><\/p>\n<p>Both systems failed on the <code>^<\/code> no-grouping test, throwing garbage into the string. The left justification item failed as well. While it could be argued that I didn&#8217;t properly use the formatting strings, this output was enough to make me distrust the function to the degree that I&#8217;d never use it.<\/p>\n<p>The premise for the <em>strfmon()<\/em> function is interesting and possibly could have value specifically for code you write that&#8217;s run in a variety of countries or regions. Alas, the placeholder documentation on the web is weak and confusing. The best documentation I found is from IBM:<\/p>\n<p><a href=\"https:\/\/www.ibm.com\/support\/knowledgecenter\/ssw_ibm_i_74\/rtref\/strfmon.htm\" rel=\"noopener\" target=\"_blank\">https:\/\/www.ibm.com\/support\/knowledgecenter\/ssw_ibm_i_74\/rtref\/strfmon.htm<\/a><\/p>\n<p>As long as international conversion isn&#8217;t required for your code, my advice is to craft your own monetary output function. A very early <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=72\" rel=\"noopener\" target=\"_blank\">Exercise post on this blog<\/a> offers some assistance in formatting output values with hundreds\/thousands separators. The rest is up to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like the <em>printf()<\/em> function, <em>strfmon<\/em> has placeholders for formatting a monetary string. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4671\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4671","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4671"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4671\/revisions"}],"predecessor-version":[{"id":4707,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4671\/revisions\/4707"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}