Wednesday 16 November 2011

How do you print a dollars-and-cents value? in C programming

How do you print a dollars-and-cents value?

The C language does not have any built-in facility for printing dollars-and-cents values. However, this omission does not present the programmer who is trying to print monetary values with an insurmountable

problem. It is quite easy to create a function that prints monetary values for you. After you create such a function, you can use it in any program you want.

Such a function is short and easy to write, and it will be presented here with a short explanation of how it works. The routine is broken into small, easy-to-write segments, making it easier to understand. The reason for breaking a program into smaller segments is discussed in Chapter XI, “Debugging.”
These routines need to use some of the standard C routines. Therefore, you need to include some header files. 
Make sure that any program that uses this routine includes these header files at the start:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
With the proper header files included, you can create a function that will accept a dollar value and print it
with commas:
void PrintDollars( double Dollars )
{
char buf[ 20 ];
int l , a;
sprintf( buf , “%01f” , Dollars );
l = strchr( buf , ‘.’ ) - buf;
for( a = ( Dollars < 0.0 ) ; a < l ; ++ a )
{
printf( “%c” , buf[ a ] );
if ( ( ( ( l - a ) % 3 ) == 1 ) && ( a != l - 1 ) )
printf( “,” );
}
}

Perhaps you’re used to seeing real numbers represented as floats. This is commonplace. Floats, however, are generally not suited for monetary work, because they suffer from a large degree of inaccuracy, such as rounding errors. Doubles are far more accurate than floats and therefore are much better suited for true numerical work. You can easily test this routine yourself by writing a program that passes it integer numbers. This routine will not, however, print decimals or “change.” To perform this task, you need to write another function specifically dedicated to this purpose:
void PrintCents( double Cents )
{
char buf[ 10 ];
sprintf( buf , “%-.02f” , Cents );
printf( “%s\n” , buf + 1 + ( Cents <= 0 ) );
}

This routine takes a decimal value and prints it correctly. Again, you can test this routine by writing a small program that passes it values.

Now you have two routines: one that prints the dollars part of a monetary value, and one that prints the cents. You certainly don’t want to have to separate each number into two parts and call each function separately!

Instead, you can make one function that accepts a monetary value, divides it into its dollars and cents components, and calls the two routines you already have. Here is that function:
void DollarsAndCents( double Amount )
{
double Dollars = Amount >= 0.0 ? floor( Amount ) : ceil( Amount
);
double Cents = Amount - (double) Dollars;
if ( Dollars < 0.0 ) printf( “-” );
printf( “$” );
PrintDollars( Dollars );
PrintCents( Cents );
}

There you have it! The DollarsAndCents routine accepts a real number (a double) and prints it to the screen in dollars-and-cents format. You probably want to test the routine. To do this, you can make a main function that attempts to print many dollars-and-cents values. Here is such a routine:

int main()
{
double num = .0123456789;
int a;
for( a = 0 ; a < 12 ; ++ a )
{
DollarsAndCents( num );
num *= 10.0;
}
return( 0 );
}

The output of the preceding program should look like this:
$0.01
$0.12
$1.23
$12.35
$123.46
$1,234.57
$12,345.68
$123,456.79
$1,234,567.89
$12,345,678.90
$123,456,789.00
$1,234,567,890.00

If you want to print monetary values differently, it is quite easy to modify this program to print numbers in a different format.

Cross Reference:

None.

No comments:

Post a Comment