{"id":4664,"date":"2021-03-20T00:01:46","date_gmt":"2021-03-20T07:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4664"},"modified":"2021-03-27T08:51:33","modified_gmt":"2021-03-27T15:51:33","slug":"exploring-the-strfmon-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4664","title":{"rendered":"Exploring the <em>strfmon()<\/em> Function"},"content":{"rendered":"<p>The C programming language doesn&#8217;t sport the thousands of functions (or methods or what-have-you) available in Java. They say Java programmers may never use or even know the variety. With many fewer functions available in the standard C library, I would think to know them all. Then along comes another one I&#8217;ve neither used nor heard of. This week&#8217;s Lesson covers such as function, <em>strfmon()<\/em>.<br \/>\n<!--more--><br \/>\nIf you&#8217;re familiar with C library naming conventions, you might figure out <em>strfmon()<\/em>: The <em>str<\/em> implies string. The <em>f<\/em> generally means formatted. The <em>mon<\/em> could be monitor, though here it stands for monetary: <em>strfmon()<\/em> is the string format for monetary values function.<\/p>\n<p>Yes, I&#8217;ve never heard of it.<\/p>\n<p>The <em>strfmon()<\/em> function takes a floating point value and applies a monetary format to it. The function works similar to the <em>printf()<\/em> function regarding the formatting process, though syntactically it&#8217;s more akin to the <em>snprintf()<\/em> function. Here is the <em>man<\/em> page format:<\/p>\n<p><code>ssize_t strfmon(char *<em>s<\/em>, size_t <em>max<\/em>, const char *<em>format<\/em>, ...);<\/code><\/p>\n<p>Buffer <code><em>s<\/em><\/code> is a <em>char<\/em> array or buffer of size <code><em>max<\/em><\/code>. It holds the string the function generates.<\/p>\n<p>The <code><em>format<\/em><\/code> argument works similar to the <em>printf()<\/em> format string, though with unique conversion characters that apply to monetary values.<\/p>\n<p>The <code>...<\/code> argument represents the values to express in monetary format.<\/p>\n<p>The <em>strfmon()<\/em> function requires that you include the <code>monetary.h<\/code> header file in your code. This function is the only function declared the header file. Not all C libraries may implement this function or have available the <code>monetary.h<\/code> header file. My Code::Blocks MinGW installation does not.<\/p>\n<p>Most importantly, the <em>strfmon()<\/em> function relies heavily upon the computer&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3541\">locale data<\/a> to properly format the monetary value. Without asserting the locale, the function&#8217;s output is unimpressive, as the following code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_20-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_03_20-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;monetary.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[32];\r\n\r\n    strfmon(buffer,32,\"%n\",359246.80);\r\n    puts(buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>strfmon()<\/em> function at Line 8 has four arguments: <code>buffer<\/code>, the 32-character storage location for the string; 32, the buffer&#8217;s size; <code>\"%n\"<\/code> which formats the string using the locale&#8217;s national format; and the value to convert.<\/p>\n<p>Here is the disappointing output:<\/p>\n<p><code>359246.80<\/code><\/p>\n<p>This example doesn&#8217;t shine because the code doesn&#8217;t know the locale. Maybe it works with some compilers, but not on the systems where I ran the test code. No, you must assert the locale before calling the function, as is done in this update:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_20-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_03_20-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,\"%n\",359246.80);\r\n    puts(buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 10, the <em>setlocale()<\/em> function is called with the locale to <code>en_US.UTF-8<\/code>, or American English. Like all good code, Line 11 tests for any errors and exits the program if one is encountered.<\/p>\n<p>Upon building this updated code, the output is more impressive:<\/p>\n<p><code>$359,246.80<\/code><\/p>\n<p>The dollar sign and comma placement are courtesy of the locale settings. These settings determine how large values are expressed and the coin of the realm.<\/p>\n<p>If you change the <em>setlocale()<\/em> function&#8217;s string to <code>en_GB.UTF-8<\/code>, here is the output:<\/p>\n<p><code>\u00a3359,246.80<\/code><\/p>\n<p>And for <code>it_IT.UTF-8<\/code>:<\/p>\n<p><code>Eu 359.246,80<\/code><\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4671\">next week&#8217;s Lesson<\/a>, I explore the <em>strfmon()<\/em> function&#8217;s format string options. Alas, my tests met with rather dismal results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It delights me to no end when I discover an unfamiliar C library function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4664\">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-4664","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\/4664","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=4664"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4664\/revisions"}],"predecessor-version":[{"id":4701,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4664\/revisions\/4701"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}