The rfmtdec() function
The rfmtdec() function uses a formatting mask to convert a decimal value to a character string.
Syntax
mint rfmtdec(dec_val, fmtstring, outbuf)
dec_t *dec_val;
char *fmtstring;
char *outbuf;
- dec_val
- A pointer to the decimal value to format.
- fmtstring
- A pointer to a character buffer that contains the formatting mask to use for the dec_val value.
- outbuf
- A pointer to a character buffer that receives the formatted string for the dec_val value.
Usage
The fmtstring argument of the rfmtdec() function points to the numeric-formatting mask, which contains characters that describe how to format the decimal value. For more information about these formatting characters, see Format numeric strings.
When you use rfmtdec() to format MONEY values, the function uses the currency symbols that the DBMONEY environment variable specifies. If you do not set this environment variable, rfmtdec() uses the currency symbols that the client locale defines. The default locale, US English, defines currency symbols as if you set DBMONEY to “$,.”. (For a discussion of DBMONEY, see theHCL Informix Guide to SQL: Reference.)
When you use a nondefault locale that has a multibyte code set, rfmtdec() supports multibyte characters in the format string. For more information, see the HCL Informix GLS User's Guide.
Return codes
- 0
- The conversion was successful.
- -1211
- The program ran out of memory (memory-allocation error).
- -1217
- The format string is too large.
Example
/*
* rfmtdec.ec *
The following program applies a series of format specifications to each
of a series of DECIMAL numbers and displays each result.
*/
#include <stdio.h>
EXEC SQL include decimal;
char *strings[] =
{
"210203.204",
"4894",
"443.334899312",
"-12344455",
0
};
char *formats[] =
{
"**###########",
"$$$$$$$$$$.##",
"(&&,&&&,&&&.)",
"<,<<<,<<<,<<<",
"$*********.**",
0
};
char result[41];
main()
{
mint x;
mint s = 0, f;
dec_t num;
printf("RFMTDEC Sample ESQL Program running.\n\n");
while(strings[s])
{
/*
* Convert each string to DECIMAL
*/
printf("String = %s\n", strings[s]);
if (x = deccvasc(strings[s], strlen(strings[s]), &num))
{
printf("Error %d in converting string [%s] to decimal\n",
x, strings[s]);
break;
}
f = 0;
while(formats[f])
{
/*
* Format DECIMAL num for each of formats[f]
*/
rfmtdec(&num, formats[f], result);
/*
* Display result and bump to next format (f++)
*/
result[40] = '\0';
printf(" Format String = '%s'\t", formats[f++]);
printf("\tResult = '%s'\n", result);
}
++s; /* bump to next string */
printf("\n"); /* separate result groups */
}
printf("\nRFMTDEC Sample Program over.\n\n");
}
Output
RFMTDEC Sample ESQL Program running.
String = 210203.204
Format String = '**###########' Result = '** 210203'
Format String = '$$$$$$$$$$.##' Result = ' $210203.20'
Format String = '(&&,&&&,&&&.)' Result = ' 000210,203. '
Format String = '<,<<<,<<<,<<<' Result = '210,203'
Format String = ' $*********.**' Result = '$***210203.20'
String = 4894
Format String = '**###########' Result = ' ** 4894'
Format String = '$$$$$$$$$$.##' Result = ' $4894.00'
Format String = '(&&,&&&,&&&.)' Result = ' 000004,894. '
Format String = '<,<<<,<<<,<<<' Result = '4,894'
Format String = ' $*********.**' Result = '$*****4894.00'
String = 443.334899312
Format String = '**###########' Result = ' ** 443'
Format String = '$$$$$$$$$$.##' Result = ' $443.33'
Format String = '(&&,&&&,&&&.)' Result = ' 0000000443. '
Format String = '<,<<<,<<<,<<<' Result = ' 443'
Format String = ' $*********.**' Result = '$******443.33'
String = -12344455
Format String = '**###########' Result = ' ** 12344455'
Format String = '$$$$$$$$$$.##' Result = ' $12344455.00'
Format String = '(&&,&&&,&&&.)' Result = '(12,344,455.)'
Format String = '<,<<<,<<<,<<<' Result = '12,344,455'
Format String = ' $*********.**' Result = ' $*12344455.00'
RFMTDEC Sample Program over.