Float to String
Need some help in converting Float into a String. Thanks!!
float calcSalesTax(float purchase, float taxRate = .06); //set the tax rate
Need to convert purchase and tax in to a string
float purchase;
cout << "Enter purchase amount: ";
cin >> purchase; //This is where the purchase response is stored
float tax = calcSalesTax(purchase); //Calculates using default rate
cout << "Purchase Amount:$"<< purchase << endl;
cout << "Tax is:$"<< tax << endl; //displaying tax
cout << "Total Sales is:$"<< purchase + tax << endl; //adding purchase and tax
[709 byte] By [
Fosterd] at [2007-11-11 7:36:18]

# 4 Re: Float to String
This is my final stab at it.
#using <mscorlib.dll> //Console use
#using <System.Windows.Forms.dll> //Use Message box
using namespace System; //Declares Indentifier "Double , Single,String...
using namespace System::Text; //StringBuilder
using namespace System::Windows::Forms; //Use of Windows forms
#include <iostream> //cin, cout, std, end1 ...
using namespace std; //cin, cout, std, end1 ...
int main()
{
// Console output:
Console::Write(S"What is your First name? ");
String * str; // This is where the response is stored
str = Console::ReadLine();
Console::Write(S"What is your Last name? ");
String * str1; //This is where the response is stored
str1 = Console::ReadLine();
float calcSalesTax(float purchase, float taxRate = .06); //set the tax rate
float purchase;
cout << "Enter purchase amount: ";
cin >> purchase; //This is where the purchase response is stored
float tax = calcSalesTax(purchase); //Calculates using default rate
///////////////////////////////////////////////////////////////////////////////
//Write to Console
Console::Write(str1); //write to screen last name
Console::Write (", " ); //place a comma
Console::Write(str); //write to screen first name
Console::WriteLine(S" "); //write a line between
////////////////////////////////////////////////////////////////////////////////
cout << "Purchase Amount:$"<< purchase << endl; //Displaying Purchase
cout << "Tax is:$"<< tax << endl; //Displaying Tax
cout << "Total Sales is:$"<< purchase + tax << endl; //Displaying Purchase and Tax
{
Double pValue; //Limit 15 digits
Single tValue; //Limit 7 digits
Double fValue;
//Declare Identtifiers
pValue = purchase;
tValue = tax;
fValue = purchase + tax;
//Use the StringBuilder class
StringBuilder * pDouble = new StringBuilder();
StringBuilder * tDouble = new StringBuilder();
StringBuilder * fDouble = new StringBuilder();
//Set 2 digit display
String * strBuffer = S"{0:f2}";
pDouble->AppendFormat(strBuffer, __box(pValue));
tDouble->AppendFormat(strBuffer, __box(tValue));
fDouble->AppendFormat(strBuffer, __box(fValue));
String * fOutput;
fOutput = String::Concat(S"\n Customer: ",str1 ,S", ",str,
S"\n Purchase Price: $", pDouble->ToString(), //Float to String using ToString
S"\n Tax: $" , tDouble->ToString(), //Display 2-digit percision
S"\n Total Cost: $", fDouble->ToString());
MessageBox::Show(fOutput,S"Reciept:"); //Write to the Message Box
}
return 0;
}
float calcSalesTax(float purchase, float taxRate)
{
return purchase * taxRate;
}