informix
Informix DataBlade API Programmer's Manual
Using Numeric Data Types

Formatting Numeric Strings

The ESQL/C library provides special functions that allow you to format numeric expressions as strings. These numeric-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 numeric-formatting masks for numeric values.

Function Name Description
rfmtdec() Converts an mi_decimal value to a string
rfmtdouble() Converts a C-language double value to a string
rfmtlong() Converts a C-language long integer value to a string

Tip: Both the ESQL/C library and the DataBlade API library provide functions to convert between mi_decimal values and other C-language data types. For more information, see Converting Decimal Data.

This section describes the characters that you can use to create a numeric- formatting mask. It also provides extensive examples that show the results of applying these masks to numeric values. A numeric-formatting mask specifies a format to apply to some numeric value. This mask is a combination of the following formatting characters:

* This character fills with asterisks any positions in the display field that would otherwise be blank.
& This character fills with zeros any positions in the display field that would otherwise be blank.
# This character changes leading zeros to blanks. Use this character to specify the maximum leftward extent of a field.
< This character left-justifies the numbers in the display field. It changes leading zeros to a null string.
, This character indicates the symbol that separates groups of three digits (counting leftward from the units position) in the whole-number part of the value. By default, this symbol is a comma. You can set the symbol with the DBMONEY environment variable. In a formatted number, this symbol appears only if the whole-number part of the value has four or more digits.
. This character indicates the symbol that separates the whole-number part of a money value from the fractional part. By default, this symbol is a period. You can set the symbol with the DBMONEY environment variable. You can have only one period in a format string.
- This character is a literal. It appears as a minus sign when expr1 is less than zero. When you group several minus signs in a row, a single minus sign floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.
+ This character is a literal. It appears as a plus sign when expr1 is greater than or equal to zero and as a minus sign when expr1 is less than zero. When you group several plus signs in a row, a single plus or minus sign floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.
( This character is a literal. It appears as a left parenthesis to the left of a negative number. It is one of the pair of accounting parentheses that replace a minus sign for a negative number. When you group several in a row, a single left parenthesis floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.
) This is one of the pair of accounting parentheses that replace a minus sign for a negative value.
$ This character displays the currency symbol that appears at the front of the numeric value. By default, the currency symbol is the dollar ($) sign. You can set the currency symbol with the DBMONEY environment variable. When you group several dollar signs in a row, a single currency symbol floats to the rightmost position that it can occupy; it does not interfere with the number.

Any other characters in the formatting mask are reproduced literally in the result.

When you use the following characters within a formatting mask, the characters float; that is, multiple occurrences of the character at the left of the pattern in the mask appear as a single character as far to the right as possible in the formatted number (without removing significant digits)

For example, if you apply the mask $$$,$$$.## to the number 1234.56, the result is $1,234.56.

When you use rfmtdec(), rfmtdouble(), or 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, the numeric-formatting functions use 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). For more information on locales, see the Informix Guide to GLS Functionality.

Figure 3-3 shows sample format strings for numeric expressions. The character b represents a blank or space.

Figure 3-3

Formatting Mask Numeric Value Formatted Result
"#####" 0 bbbbb
"&&&&&" 0 00000
"$$$$$" 0 bbbb$
"*****" 0 *****
"<<<<<" 0 (null string)
"##,###" 12345 12,345
"##,###" 1234 b1,234
"##,###" 123 bbb123
"##,###" 12 bbbb12
"##,###" 1 bbbbb1
"##,###" -1 bbbbb1
"##,###" 0 bbbbbb
"&&,&&&" 12345 12,345
"&&,&&&" 1234 01,234
"&&,&&&" 123 000123
"&&,&&&" 12 000012
"&&,&&&" 1 000001
"&&,&&&" -1 000001
"&&,&&&" 0 000000
"$$,$$$" 12345 ***** (overflow)
"$$,$$$" 1234 $1,234
"$$,$$$" 123 bb$123
"$$,$$$" 12 bbb$12
"$$,$$$" 1 bbbb$1
"$$,$$$" -1 bbbb$1
"$$,$$$" 0 bbbbb$
"$$,$$$"
(DBMONEY set to DM)
1234 DM1,234
"**,***" 12345 12,345
"**,***" 1234 *1,234
"**,***" 123 ***123
"**,***" 12 ****12
"**,***" 1 *****1
"**,***" 0 ******
"##,###.##" 12345.67 12,345.67
"##,###.##" 1234.56 b1,234.56
"##,###.##" 123.45 bbb123.45
"##,###.##" 12.34 bbbb12.34
"##,###.##" 1.23 bbbbb1.23
"##,###.##" 0.12 bbbbbb.12
"##,###.##" 0.01 bbbbbb.01
"##,###.##" -0.01 bbbbbb.01
"##,###.##" -1 bbbbb1.00
"&&,&&&.&&" .67 000000.67
"&&,&&&.&&" 1234.56 01,234.56
"&&,&&&.&&" 123.45 000123.45
"&&,&&&.&&" 0.01 000000.01
"$$,$$$.$$" 12345.67 ********* (overflow)
"$$,$$$.$$" 1234.56 $1,234.56
"$$,$$$.##" 0.00 bbbbb$.00
"$$,$$$.##" 1234.00 $1,234.00
"$$,$$$.&&" 0.00 bbbbb$.00
"$$,$$$.&&" 1234.00 $1,234.00
"-##,###.##" -12345.67 -12,345.67
"-##,###.##" -123.45 -bbb123.45
"-##,###.##" -12.34 -bbbb12.34
"--#,###.##" -12.34 b-bbb12.34
"---,###.##" -12.34 bb-bb12.34
"---,-##.##" -12.34 bbbb-12.34
"---,--#.##" -12.34 bbbb-12.34
"--#,###.##" -1.00 b-bbbb1.00
"---,--#.##" -1.00 bbbbb-1.00
"-##,###.##" 12345.67 b12,345.67
"-##,###.##" 1234.56 bb1,234.56
"-##,###.##" 123.45 bbbb123.45
"-##,###.##" 12.34 bbbbb12.34
"--#,###.##" 12.34 bbbbb12.34
"---,###.##" 12.34 bbbbb12.34
"---,-##.##" 12.34 bbbbb12.34
"---,---.##" 1.00 bbbbbb1.00
"---,---.--" -.01 bbbbbb-.01
"---,---.&&" -.01 bbbbbb-.01
"-$$$,$$$.&&" -12345.67 -$12,345.67
"-$$$,$$$.&&" -1234.56 -b$1,234.56
"-$$$,$$$.&&" -123.45 -bbb$123.45
"--$$,$$$.&&" -12345.67 -$12,345.67
"--$$,$$$.&&" -1234.56 b-$1,234.56
"--$$,$$$.&&" -123.45 b-bb$123.45
"--$$,$$$.&&" -12.34 b-bbb$12.34
"--$$,$$$.&&" -1.23 b-bbbb$1.23
"----,--$.&&" -12345.67 -$12,345.67
"----,--$.&&" -1234.56 b-$1,234.56
"----,--$.&&" -123.45 bbb-$123.45
"----,--$.&&" -12.34 bbbb-$12.34
"----,--$.&&" -1.23 bbbbb-$1.23
"----,--$.&&" -.12 bbbbbb-$.12
"$***,***.&&" 12345.67 $*12,345.67
"$***,***.&&" 1234.56 $**1,234.56
"$***,***.&&" 123.45 $****123.45
"$***,***.&&" 12.34 $*****12.34
"$***,***.&&" 1.23 $******1.23
"$***,***.&&" .12 $*******.12
"($$$,$$$.&&)" -12345.67 ($12,345.67)
"($$$,$$$.&&)" -1234.56 (b$1,234.56)
"($$$,$$$.&&)" -123.45 (bbb$123.45)
"(($$,$$$.&&)" -12345.67 ($12,345.67)
"(($$,$$$.&&)" -1234.56 b($1,234.56)
"(($$,$$$.&&)" -123.45 b(bb$123.45)
"(($$,$$$.&&)" -12.34 b(bbb$12.34)
"(($$,$$$.&&)" -1.23 b(bbbb$1.23)
"((((,(($.&&)" -12345.67 ($12,345.67)
"((((,(($.&&)" -1234.56 b($1,234.56)
"((((,(($.&&)" -123.45 bbb($123.45)
"((((,(($.&&)" -12.34 bbbb($12.34)
"((((,(($.&&)" -1.23 bbbbb($1.23)
"((((,(($.&&)" -.12 bbbbbb($.12)
"($$$,$$$.&&)" 12345.67 b$12,345.67
"($$$,$$$.&&)" 1234.56 bb$1,234.56
"($$$,$$$.&&)" 123.45 bbbb$123.45
"(($$,$$$.&&)" 12345.67 b$12,345.67
"(($$,$$$.&&)" 1234.56 bb$1,234.56
"(($$,$$$.&&)" 123.45 bbbb$123.45
"(($$,$$$.&&)" 12.34 bbbbb$12.34
"(($$,$$$.&&)" 1.23 bbbbbb$1.23
"((((,(($.&&)" 12345.67 b$12,345.67
"((((,(($.&&)" 1234.56 bb$1,234.56
"((((,(($.&&)" 123.45 bbbb$123.45
"((((,(($.&&)" 12.34 bbbbb$12.34
"((((,(($.&&)" 1.23 bbbbbb$1.23
"((((,(($.&&)" .12 bbbbbbb$.12
"<<<,<<<" 12345 12,345
"<<<,<<<" 1234 1,234
"<<<,<<<" 123 123
"<<<,<<<" 12 12
Sample Format Patterns and Their Results


Informix DataBlade API Programmer's Manual, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved