Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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]
# 1 Re: Float to String
You can use the sstream library as shown here:
http://www.dev-archive.com/dev-archive/LegacyLink/9399
Or use the <stdio> functions sprintf() and such.
Danny at 2007-11-11 21:02:07 >
# 2 Re: Float to String
this question came at a goog time, I'm at the point where I need the exact same thing.

http://gethelp.dev-archive.com/techtips/cpp_pro/10min/2001/april/10min0401-2.asp

string val;
s >> value; // should be val?

Now val contains the string "123.45".

The opposite conversion, namely double to string, is just as simple:

should be "string to double"?
rssmps at 2007-11-11 21:03:07 >
# 3 Re: Float to String
yes and yes:)

I'll ask my editor to fix it.
Danny at 2007-11-11 21:04:12 >
# 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;


}
Fosterd at 2007-11-11 21:05:07 >
# 5 Re: Float to String
Danny,

question on the string to val.
I'm using a float variable and the string was 2.57. However, the conversion made it 2.569998.
While it's not THAT much different, it is "less" than the 2.57 value which will cause some problems for me.

Will it be more exact if I use double or is this something that I'll have to live with? If this is something that can't be avoided, I'll have to put in a compare and say if the values are less than a certain % different, it's considered the same value. Only problem is how tight do you define that difference?
rssmps at 2007-11-11 21:06:06 >
# 6 Re: Float to String
Using double, or better yet, long double will reduce the deviation but won't eliminate the problem altogether. There are several workarounds but none of them is ideal. First, don't use == directly to compare two floating pointer values but check whether the difference between them doesn't exceed a certain fractional part. Alternatively, you can use the Epsilon constant to represent this deviation. Finally, if you need to store numeric values such as prices, salaries etc., use scaled integeres: int for the integral part and another int for the fraction, e.g., 103 and 98 for the value $103.98.
I discuss this technique in length here:
http://www.dev-archive.com/cplus/10MinuteSolution/21016
Danny at 2007-11-11 21:07:06 >