-
May 16th, 2014, 07:47 PM
#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);
}
}
-
May 17th, 2014, 01:41 AM
#2
Re: Help converting float to string
Victor Nijegorodov
-
May 17th, 2014, 05:05 AM
#3
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)
-
May 20th, 2014, 01:57 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|