Return the rightmost digit of a DBMONEY value as a DBCHAR.
RETCODE dbmnyndigit(dbproc, mnyptr, value, zero) DBPROCESS *dbproc; DBMONEY *mnyptr; DBCHAR *value; DBBOOL *zero;
A pointer to the DBPROCESS structure that provides the connection for a particular front-end/server process. It contains all the information that DB-Library uses to manage communications and data between the front end and the server.
This parameter may be NULL. The DBPROCESS is used as a parameter to an application’s error handler. It also contains information on what language to print error messages in. If a DBPROCESS is not supplied, the default national language is used.
A pointer to a DBMONEY value. Each call to dbmnyndigit divides this value by 10 and places the result back into *mnyptr.
A pointer to a DBCHAR variable to fill with the character representation of the rightmost digit of the DBMONEY value.
A pointer to a DBBOOL variable. Each call to dbmnyndigit divides *mnyptr by 10 and puts the character representation of the remainder of the division in *value. If the result of the division is $0.0000, dbmnyndigit sets *zero to “true”. Otherwise, *zero is set to “false”. If zero is passed as NULL, this information is not returned.
SUCCEED or FAIL.
dbmnyndigit returns FAIL if mnyptr or value is NULL.
dbmnyndigit returns the rightmost digit of a DBMONEY value as a DBCHAR.
dbmnyndigit divides a DBMONEY value by 10. It places the character representation of the remainder of the division in *value, and replaces *mnyptr with the result of the division. If the result of the division is $0.0000, dbmnyndigit sets *zero to “true”.
To get all the digits of a DBMONEY value, call dbmnydigit repeatedly, until *zero is “true”.
dbmnyinit and dbmnyndigit are useful for writing a custom DBMONEY-to-DBCHAR conversion routine. Such a custom routine might be useful if the accuracy provided by dbconvert’s DBMONEY-to-DBCHAR conversion (hundredths of a dollar) is not adequate. Also dbconvert does not build a character string containing commas.
dbmnyinit initializes a DBMONEY value for conversion to character. It eliminates unwanted precision and converts negative values to positive. See the dbmnyinit reference page.
The range of legal DBMONEY values is between +/-$922,337,203,685,477.5808. DBMONEY values have a precision of one ten-thousandth of a dollar.
This code fragment demonstrates the use of dbmnyndigit and dbmnyinit:
/*
** This example demonstrates dbmnyinit() and
** dbmnyndigit(). It is a conversion routine which
** converts a DBMONEY value to a character string.
** The conversion provided by this routine is unlike
** the conversion provided by dbconvert() in that the
** resulting character string includes commas. This
** conversion provides precision of two digits after
** the decimal point.
**
** For simplicity, the example assumes that all
** routines succeed and all parameters passed to it
** are valid.
*/
#define PRECISION 2
RETCODE new_mnytochar(mnyptr, buf_ptr)
DBMONEY *mnyptr;
char *buf_ptr;
{
DBMONEY local_mny;
DBBOOL negative;
int bytes_written;
DBCHAR value;
DBBOOL zero;
int ret;
char temp_buf[32];
/*
** Since dbmnyinit() and dbmnyndigit() modify the
** DBMONEY value passed to it, and since we do
** not want to modify the DBMONEY value passed
** to us by the user we need to make a local copy.
*/
ret = dbmnycopy((DBPROCESS *)NULL, mnyptr,
&local_mny);
/* The value of ’ret’ should be checked */
/*
** Next we need to call dbmnyinit().
**
** dbmnyinit() eliminates any unwanted precision
** from the DBMONEY value. DBMONEY values are
** stored with accuracy to four digits after the
** decimal point. For this conversion routine we
** only want accuracy to two digits after the
** decimal.
**
** Passing a value of 2 for the second parameter
** eliminates those two digits of precision we do
** not care about.
**
** dbmnyinit() also turns negative DBMONEY values
** into positive DBMONEY values. The value of
** negative is set to TRUE if dbmnyinit() turns a
** negative DBMONEY value into a positive DBMONEY
** value.
**
** NOTE: dbmnyinit() eliminates unwanted by
** precision by dividing DBMONEY values by a
** power of ten. In this conversion routine it
** divides by 100. If we pass dbmnyinit() a
** DBMONEY value of $1534.1277 the resulting
** DBMONEY value is $15.3413.
*/
negative = FALSE;
ret = dbmnyinit((DBPROCESS *)NULL, &local_mny,
4 - PRECISION, &negative);
/* The value of ’ret’ should be checked */
/*
** dbmnyndigit() extracts the rightmost digit out
** of the DBMONEY value, converts it to a
** character, places the character into the
** variable “value”, and then divides the DBMONEY
** value by 10. dbmnyndigit() sets ’zero’ to TRUE
** if the result of the division is $0.0000.
**
** By calling dbmnyndigit() until ’zero’ is set to
** TRUE we will be returned all the digits (from
** right to left) of the DBMONEY value.
*/
zero = FALSE;
bytes_written = 0;
while( zero == FALSE )
{
ret = dbmnyndigit((DBPROCESS *)NULL,
&local_mny, &value, &zero);
/* The value of ’ret’ should be checked. */
/*
** As we are getting the digits, we want to
** place the decimal point and commas in the
** proper positions ...
*/
temp_buf[bytes_written++] = value;
/*
** If zero == TRUE we got all the digits. We
** do not want to call
** check_comma_and_decimal() since we might
** put a comma before the leftmost digit.
*/
if( zero == FALSE )
{
/*
** As we are getting the digits, we want
** to place the decimal point and commas
** in the proper positions ...
*/
check_comma_and_decimal(temp_buf,
&bytes_written);
}
}
/*
** If we haven’t written PRECISION bytes into the
** buffer yet, pad with zeros, write the decimal
** point to the buffer, and write a zero after
** the decimal point.
*/
pad_with_zeros(temp_buf, &bytes_written);
/*
** We’ve written the money value into the buffer
** backwards. Now we have to write it the right
** way.
*/
reverse_money(buf_ptr, temp_buf, bytes_written,
negative);
return(SUCCEED);
}
void check_comma_and_decimal(temp_buf,
bytes_written)
char *temp_buf;
int *bytes_written;
{
static int comma = 0;
static DBBOOL after_decimal = FALSE;
if( after_decimal )
{
/*
** When comma is 3 it is time to write a
** comma. We do not care about commas until
** after we’ve written the decimal point.
*/
comma++;
}
/*
** After we’ve written PRECISION bytes into the
** buffer, it’s time to write the decimal point.
*/
if( *bytes_written == PRECISION )
{
temp_buf[(*bytes_written)++] = ’.’;
after_decimal = TRUE;
}
/*
** When (comma == 3) that means we’ve written three
** digits and it’s time to put a comma into the
** buffer.
*/
if( comma == 3 )
{
temp_buf[(*bytes_written)++] = ’,’;
comma = 0; /* clear comma */
}
}
void pad_with_zeros(temp_buf, bytes_written)
char *temp_buf;
int *bytes_written;
{
/* If we haven’t written PRECISION bytes into the
** buffer yet, pad with zeros, write the decimal
** point to the buffer, and write a zero after the
** decimal point.
*/
while( *bytes_written < PRECISION )
{
temp_buf[(*bytes_written)++] = ’0’;
}
if( *bytes_written == PRECISION )
{
temp_buf[(*bytes_written)++] = ’.’;
temp_buf[(*bytes_written)++] = ’0’;
}
}
void reverse_money(char_buf, temp_buf,
bytes_written, negative)
char *char_buf;
char *temp_buf;
int bytes_written;
DBBOOL negative;
{
int i;
/*
** We’ve written the money value into the buffer
** backwards. Now we have to write it the right
** way. First check to see if we need to write a
** negative sign, then write the dollar sign,
** finally write the money value.
*/
i = 0;
if( negative == TRUE )
{
char_buf[i++] = ’-’;
}
char_buf[i++] = ’$’;
while( bytes_written-- )
{
char_buf[i++] = temp_buf[bytes_written];
}
/* Append null-terminator: */
char_buf[i] = ’\0’;
}