CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2014
    Posts
    1

    Help converting float to string

    I have an assignment that is due on monday I am stuck on this function. I have to convert my netpay which is a float to a string So if i have 356.26
    it should output the sum of three hundred fifty-six and 26/100 dollars
    my program function works for the sum of three hundred but after that it spits out garbage.
    PLEASE HELP.

    Code:
    void convertNetPay(float netPay, char *netPayString)
    {
    	int numHuns, numTens, numOnes;
    	char OnesTable[9][8]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    	char ResultString[10+1];
    	char TensTable[9][8] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    	char TeensTable[9][10] ={"Eleven", "Twelve","Thirteen","fourteen","fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    	float cents;
    			  
    	cents = netPay - int(netPay);
    
    	strcpy(netPayString,"The sum of ");
    
    	numHuns = int(netPay) / 100;
    
    	if (numHuns > 0)
    	{
    	strcat(netPayString,OnesTable[numHuns-1]);
    	strcat(netPayString," Hundred ");
    	
    	
    	}
    	int remainder =  int(netPay) % 100;
    	/*if  (remainder ==0)
    	{
    		//do sumthing
    	}*/
    	
    		 if ((remainder<=11) || (remainder>=19))
    		{
    
    			strcat(netPayString, TeensTable[remainder -11]);
    		}
    			else{
    
    				numTens = int(netPay) % 100 / 10;
    
    
    				numOnes = int(netPay) % 100 % 10;
    				if (numTens > 0)
    				{
    					strcat(netPayString,TensTable[numTens -1]);
    					strcat(netPayString," - ");
    				}
    				if (numOnes > 0)
    				{
    					strcat(netPayString,OnesTable[numOnes-1]);
    					strcat(netPayString," Dollars and ");
    				}
    			
    			cents = cents + 0.005;
    
    			cents = cents * 100;
    			cents=int(cents);
    			sprintf(ResultString,"%d",cents);
    			strcat(netPayString,ResultString);
    			}
    	
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Help converting float to string

    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Help converting float to string

    Code:
    float cents;
    ...
    sprintf(ResultString,"%d",cents);
    Does not compute!

    cents is defined as a float. Even though you try to make it an int using a cast, its type is still a float.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: Help converting float to string

    Float to String
    Code:
    float f = 5.5;
    CString s;
    s.Format("%0.2f", f);
    ::AfxMessageBox(s);
    Try this.
    Regards,

    SaraswathiSrinath

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured