Numeric-Formatting Functions
INFORMIX-ESQL/C provides special functions that allow you to format numeric expressions. These formatting functions apply a given formatting mask to a numeric value to allow you to line up decimal points, right- or left-justify the number, enclose a negative number in parentheses, and so on. The ESQL/C library includes the following functions that support formatting masks for numeric values.
When you compile your ESQL/C program with the esql command, the preprocessor automatically links these functions to your program.
This chapter describes the characters that you can use to create a formatting mask. It also provides extensive examples that show the results of applying these masks to numeric values.
rfmtdec()
The rfmtdec() function uses a formatting mask to convert a decimal value to a character string.
Syntax
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 on these formatting characters, see "Formatting 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, U.S. English, defines currency symbols as if you set DBMONEY to "$,. ". (For a discussion of DBMONEY, see the 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 Informix Guide to GLS Functionality. 
Return Codes
Example
The demo directory contains this sample program in the file rfmtdec.ec.
/*
* 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()
{
int x;
int 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))
{
Example 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'
rfmtdouble()
The rfmtdouble() function uses a formatting mask to convert a double value to a character string.
Syntax
Usage
The fmtstring argument of the rfmtdouble() function points to the numeric-formatting mask, which contains characters that describe how to format the double value. For more information on these formatting characters, see "Formatting Numeric Strings".
When you use rfmtdouble() to format MONEY values, the function uses the currency symbols that the DBMONEY environment variable specifies. If you do not set this environment variable, rfmtdouble() uses the currency symbols that the client locale defines. The default locale, U.S. English, defines currency symbols as if you set DBMONEY to "$,. ". (For a discussion of DBMONEY, see the Informix Guide to SQL: Reference).
When you use a nondefault locale that has a multibyte code set, rfmtdouble() supports multibyte characters in the format string. For more information, see the Informix Guide to GLS Functionality. 
Return Codes
Example
The demo directory contains this sample program in the file rfmtdouble.ec.
double dbls[] =
{
210203.204,
4894,
443.334899312,
-12344455,
0
};
char *formats[] =
{
"#############",
"<,<<<,<<<,<<<",
"$$$$$$$$$$.##",
"(&&,&&&,&&&.)",
"$*********.**",
0
};
char result[41];
main()
{
int x;
int i = 0, f;
printf("RFMTDOUBLE Sample ESQL Program running.\n\n");
while(dbls[i]) /* for each number in dbls */
{
printf("Double Number = %g\n", dbls[i]);
f = 0;
while(formats[f]) /* format with each of formats[] */
{
if (x = rfmtdouble(dbls[i], formats[f], result))
{
printf("Error %d in formating %g using %s\n",
x, dbls[i], formats[f]);
break;
}
/*
* Display each result and bump to next format (f++)
*/
result[40] = '\0';
printf(" Format String = '%s'\t", formats[f++]);
printf("\tResult = '%s'\n", result);
}
++i; /* bump to next double */
printf("\n"); /* separate result groups */
}
printf("\nRFMTDOUBLE Sample Program over.\n\n");
}
Example Output
RFMTDOUBLE Sample ESQL Program running.
Double Number = 210203
Format String = '#############' Result = ' 210203'
Format String = '<,<<<,<<<,<<<' Result = '210,203'
Format String = '$$$$$$$$$$.##' Result = ' $210203.20'
Format String = '(&&,&&&,&&&.)' Result = ' 000210,203. '
Format String = '$*********.**' Result = '$***210203.20'
Double Number = 4894
Format String = '#############' Result = ' 4894'
Format String = '<,<<<,<<<,<<<' Result = '4,894'
Format String = '$$$$$$$$$$.##' Result = ' $4894.00'
Format String = '(&&,&&&,&&&.)' Result = ' 000004,894. '
Format String = '$*********.**' Result = '$*****4894.00'
Double Number = 443.335
Format String = '#############' Result = ' 443'
Format String = '<,<<<,<<<,<<<' Result = '443'
Format String = '$$$$$$$$$$.##' Result = ' $443.33'
Format String = '(&&,&&&,&&&.)' Result = ' 0000000443. '
Format String = '$*********.**' Result = '$******443.33'
Double Number = -1.23445e+07
Format String = '#############' Result = ' 12344455'
Format String = '<,<<<,<<<,<<<' Result = '12,344,455'
Format String = '$$$$$$$$$$.##' Result = ' $12344455.00'
Format String = '(&&,&&&,&&&.)' Result = '(12,344,455.)'
Format String = '$*********.**' Result = '$*12344455.00'
RFMTDOUBLE Sample Program over.
rfmtlong()
The rfmtlong() function uses a formatting mask to convert a C long value to a character string.
Syntax
Usage
The fmtstring argument of the rfmtlong() function points to the numeric-formatting mask, which contains characters that describe how to format the long integer value. For more information on these formatting characters, see "Formatting Numeric Strings".
When you use rfmtlong() to format MONEY values, the function uses the currency symbols that the DBMONEY environment variable specifies. If you do not set this environment variable, rfmtlong() uses the currency symbols that the client locale defines. The default locale, U.S. English, defines currency symbols as if you set DBMONEY to "$,. ". (For a discussion of DBMONEY, see the Informix Guide to SQL: Reference).
When you use a nondefault locale that has a multibyte code set, rfmtlong() supports multibyte characters in the format string. For more information, see the Informix Guide to GLS Functionality. 
Return Codes
Example
The demo directory contains this sample program in the file rfmtlong.ec.
long lngs[] =
{
21020304L,
334899312L,
-334899312L,
-12344455L,
0
};
char *formats[] =
{
"################",
"$$$$$$$$$$$$$.##",
"(&,&&&,&&&,&&&.)",
"<<<<,<<<,<<<,<<<",
"$************.**",
0
};
char result[41];
main()
{
int x;
int s = 0, f;
printf("RFMTLONG Sample ESQL Program running.\n\n");
while(lngs[s]) /* for each long in lngs[] */
{
printf("Long Number = %ld\n", lngs[s]);
f = 0;
while(formats[f]) /* format with each of formats[] */
{
if (x = rfmtlong(lngs[s], formats[f], result))
{
printf("Error %d in formatting %ld using %s.\n",
x, lngs[s], formats[f]);
break;
}
/*
* 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 long */
printf("\n"); /* separate display groups */
}
printf("\nRFMTLONG Sample Program over.\n\n");
}
Example Output
RFMTLONG ESQL Sample Program running.
Long Number = 21020304
Format String = '################' Result = ' 21020304'
Format String = '$$$$$$$$$$$$$.##' Result = ' $21020304.00'
Format String = '(&,&&&,&&&,&&&.)' Result = ' 00021,020,304. '
Format String = '<<<<,<<<,<<<,<<<' Result = '21,020,304'
Format String = '$************.**' Result = '$****21020304.00'
Long Number = 334899312
Format String = '################' Result = ' 334899312'
Format String = '$$$$$$$$$$$$$.##' Result = ' $334899312.00'
Format String = '(&,&&&,&&&,&&&.)' Result = ' 00334,899,312. '
Format String = '<<<<,<<<,<<<,<<<' Result = '334,899,312'
Format String = '$************.**' Result = '$***334899312.00'
Long Number = -334899312
Format String = '################' Result = ' 334899312'
Format String = '$$$$$$$$$$$$$.##' Result = ' $334899312.00'
Format String = '(&,&&&,&&&,&&&.)' Result = '(00334,899,312.)'
Format String = '<<<<,<<<,<<<,<<<' Result = '334,899,312'
Format String = '$************.**' Result = '$***334899312.00'
Long Number = -12344455
Format String = '################' Result = ' 12344455'
Format String = '$$$$$$$$$$$$$.##' Result = ' $12344455.00'
Format String = '(&,&&&,&&&,&&&.)' Result = '(00012,344,455.)'
Format String = '<<<<,<<<,<<<,<<<' Result = '12,344,455'
Format String = '$************.**' Result = ' $****12344455.00'
RFMTLONG Sample Program over.
|