Solved

Need to format currency using C

Posted on 2000-03-28
5
295 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:8088MMX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 1

Expert Comment

by:940961sl
ID: 2665691
#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
 

Accepted Solution

by:
euy earned 175 total points
ID: 2675578
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
 
LVL 1

Author Comment

by:8088MMX
ID: 2680858
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
 
LVL 1

Author Comment

by:8088MMX
ID: 2680867
EUY,
I changed my mind gave you all the points anyway. I guess I'm generous.
0
 
LVL 1

Author Comment

by:8088MMX
ID: 2680875
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question