I wrote a couple functions to do what I suggested, although I didn't test it very extensively, so use at your own risk:
#include <stdio.h>
#define NUM_CHARS 8
void floating_dollar(float f);
int num_chars_float(float f);
int
main()
{
int i;
float f[5] = { 1.1, 0.5, 13.8, 102.19, 4172.91 };
for (i = 0; i < 5; i++)
floating_dollar(f[i]);
return 0;
}
void
floating_dollar(float f)
{
int count;
int i;
count = num_chars_float(f);
for (i = 0; i < NUM_CHARS - count; i++)
putchar(' ');
printf("$%.2f\n",f);
}
int
num_chars_float(float f)
{
int num_chars;
num_chars = 0;
if (f < 1)
return 1;
while(1) {
if ((int) f == 0)
return num_chars;
num_chars++;
f /= 10;
}
}