Function Accepts double and return string(NEED HELP)!
I'm new to programing and to C++, got a bit lost with this function!
I need to write a global function which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function.
All sugestions are very welcome,
Thansk a lot.
# 1 Re: Function Accepts double and return string(NEED HELP)!
You probably need to use stringstream objects for the conversion to string. With respect to the formatting: do you need only to control the number of digits after the decimal point? This should be easy. you need to multiply the double by 10^digits, e.g., for three digits after the decimal point multiply the value by 10^3=1000. The convert the result to int. This will trim the additional decimal digits. Next, use the modulo operator to separate the integral part from the fraction, and insert it as three components to the string:
s<<integral_part<<"."<<fraction.
Danny at 2007-11-11 21:01:10 >

# 2 Re: Function Accepts double and return string(NEED HELP)!
Danny, thanks for you help. But I am new to programming and eventhough yourexplanation was clear I still didn't make it work. Now , Is it too much if you give some samples codes on this subject so I can have a better understand? Appriciate you help.
Thanks
# 3 Re: Function Accepts double and return string(NEED HELP)!
This is pretty simple if used with stringstream ... here what you can do !!
string funct(double arg1,int arg2=2) {
ostringstream out;
out<<setprecision(arg2)<<arg1;
return out.str();
}
This should work fine on C++.
# 4 Re: Function Accepts double and return string(NEED HELP)!
OK, I made a few changes and improvements to your code. Here's the result. It's working:
#include <string>
#include <sstream>
using namespace std;
string funct(double arg1,int decplaces=3) {
ostringstream out;
out.precision(16);//override the default which is 6
out<<arg1;
string str= out.str(); //extract string from stream
size_t n=str.find('.');
if ((n!=string::npos)// is there a decimal point?
&& (str.size()> n+decplaces)) //is it followed by at least decplaces more digits?
{
str[n+decplaces]='\0'; //put a nul after decplaces
}
str.swap(string(str.c_str()) ); //get rid of spurious characters after the nul
return str;
}
int main()
{
string s=
funct(15.6999);
s=
funct(11115.6999);
s=
funct(5.0001);
s=
funct(11115.6999);
s=
funct(123456789.69999001);
}
Danny at 2007-11-11 21:04:08 >

# 5 Re: Function Accepts double and return string(NEED HELP)!
Thanks Danny for your help, I also read a article you wrote on converting string which was very helpful. However, the code you gave me is giving the following error:
I am using a Dev-C++ compiler, is it the problem?
str.swap(string(str.c_str()) );
22 C:\Cpp2Work\doubleToString2.cpp no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::swap(std::string
# 6 Re: Function Accepts double and return string(NEED HELP)!
You're probably using a very old compiler which doesn't support default template arguments. You can use replace the swap call with a sequence of assignment expressions that overrides the current string value:
string temp = str.c_str();
str=temp;
Danny at 2007-11-11 21:06:12 >
