Need to format currency using C

I need you to write me a C function that receives as input a FLOAT called "money" and prints out (printf)the money ammount in the format $XXX,XXX,XXX,XXX.XX
you should assume that "money <= 999999999999.99". I also need the dollar sign to trail in front of the amount with only 1 space between the sign and the amount.
Please note that i do not want the dollar sign in a fixed position, it should trail in front of the amount. For example, if "money = 200000.99" the output of the function should be "$ 200,000.00" and not "$    ,   ,200,000.00"
the output is just a printf.
LVL 1
8088MMXAsked:
Who is Participating?
 
euyConnect With a Mentor Commented:
Hello, I have here a code which does what you wanted. You may want to take a look at it. Just compile this one and see for yourself. I hope this would help.



#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>

#define TRUE   1
#define FALSE  0

void Operate_Buff(int typeflag, int digitCntr, int comma_cntr);
int Get_commas(int numCnt);

char Buff[50];
char NewVal[50];
int x,y,cntr;
int dotFlag;


void main()
{
      int digitCntr;
      int whole_num_cntr;
      int comma_cntr;
      int with_decimal_flag;

      digitCntr = 0;
      whole_num_cntr = 0;
      dotFlag = FALSE;
      with_decimal_flag = FALSE;
      memset(Buff, 0x00, 50);
      memset(NewVal, 0x00, 50);

      printf("\nPlease enter amount : ");
      gets(Buff);

      /* get no. of digits */
      digitCntr = strlen(Buff);

      for(x=0; x<digitCntr; x++)
      {
            if(Buff[x]=='.')
            {
                  with_decimal_flag = TRUE;
                  break;
            }
            whole_num_cntr++;
      }
      comma_cntr = Get_commas(whole_num_cntr);
      Operate_Buff(with_decimal_flag, digitCntr, comma_cntr);

      /* display output */
      printf("\nThe amount is %s\n\n\n", NewVal);  
}


int Get_commas(int numCnt)
{
      int commas;

      if(numCnt <= 3)
            commas = 0;
      else if(((numCnt%3)==0)&&(numCnt!=3))
            commas = (numCnt/3)-1;
      else
            commas = numCnt/3;

      return(commas);
}



void Operate_Buff(int typeflag, int digitCntr, int comma_cntr)
{
      /*******************************
      *    operation on Buff here    *
      *******************************/
      y=(digitCntr-1)+2+comma_cntr;
      cntr=0;

      /* has the decimal point */
      if(typeflag == TRUE)
      {
            for(x=(digitCntr-1); x >= 0; x--)
            {
                  if(dotFlag==TRUE)
                        cntr++;

                  if(dotFlag==FALSE)
                  {
                        if(Buff[x]=='.')
                              dotFlag = TRUE;
                  }

                  NewVal[y] = Buff[x];
                  y--;
                  if(cntr==3)
                  {
                        if(x!=0)
                        {
                              NewVal[y] = ',';
                              y--;
                              cntr = 0;
                        }
                        continue;
                  }
            }
      }

      /* whole number only */
      else  
      {
            for(x=(digitCntr-1); x >= 0; x--)
            {
                  cntr++;
                  NewVal[y] = Buff[x];
                  y--;
                  if(cntr==3)
                  {
                        if(x!=0)
                        {
                              NewVal[y] = ',';
                              y--;
                              cntr = 0;
                        }
                        continue;
                  }
            }
      }

      NewVal[0] = '$';
      NewVal[1] = ' ';
}

0
 
940961slCommented:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void MoneyFloatToString(double Money)
{
   char                                Buffer[255];
   int                                 Position, Counter;

   sprintf(Buffer, "$ ");
   sprintf(&Buffer[2], "%f", Money);
   for (Position = 0; Buffer[Position] != '.'; Position++);
   Buffer[Position+3] = 0;

   for (;Position > 5; Position -= 3)
   {
      for (Counter = strlen(Buffer) + 1; Counter > Position - 4; Buffer[Counter+1]=Buffer[Counter--]);
      Buffer[++Counter] = ',';
   }

   printf("%s\n", Buffer);
}

void main (void)
{
   MoneyFloatToString(999999999999.9999);
   MoneyFloatToString(123456789012.3456);
}
0
 
8088MMXAuthor Commented:
Stefaan (940961sl),

Thank you for answering my question so fast and well. It is a very efficient piece of code. I cannot give you the points in this question because EUY posted an ANSWER for it and you just posted a COMMENT. However, you deserve the points, so I created a question that only you can answer. Go to this link and post an answer and I will give you a grade letter of A and 175 points.
http://www.experts-exchange.com/bin/InputUser?email=miguel%40its%2emsstate%2eedu&destination=/EQ.10322099 



EUY,
I don't know if your answer to my question works or not, because I did not try it and I don't want to try it.
The code you sent me, is hard to understand, lenghty, and most of all very ineficient because it uses so many variables. Besides that 940961sl add already posted a correct answer before you (as a comment).
However, I am going to give you 30 points for the effort.
0
 
8088MMXAuthor Commented:
EUY,
I changed my mind gave you all the points anyway. I guess I'm generous.
0
 
8088MMXAuthor Commented:
Stefaan,
You can also get to the question if you go to http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=10322099 

0
All Courses

From novice to tech pro — start learning today.