Help converting float to string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Help converting float to string

  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
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Help converting float to string

    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center