question: std:string and float values??

W

Woodster

I am starting to use templates and have managed to figure out how to use
std::string, std::map and make_pair successfully so far (Yeah I know -
not much of a big step but I'm getting there)

Previously I was using something like:

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?

Thanks in advance

Sean Hannan
 
S

Senthilvel Samatharman

Woodster said:
I am starting to use templates and have managed to figure out how to use
std::string, std::map and make_pair successfully so far (Yeah I know -
not much of a big step but I'm getting there)

Previously I was using something like:

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?

Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());

std::cout<<buffer;

return 0;
}

This is how i'd do .. mabe experts here can show you a better way..
 
W

Woodster

Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());

Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'

Sean Hannan
 
F

Frank Schmitt

Woodster said:
Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'

use std::setprecision:

#include <iomanip>
#include <sstream>

int main() {
std:: stringstream str;
double value = 10.5;

str << std::setprecision(2) << "text " << value << " text";
string buffer(str.str());
....

HTH & kind regards
frank
 
S

Senthilvel Samatharman

Woodster said:
Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'

Use setprecision of <iomanip>
Think this is what you ask for

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int main()
{
std:: stringstream str;
double value = 10.590878;

str<<"text "<<std::setprecision(5)<<value<<" text"; //--> value will have
exactly five digits( before decimal point + after decimal point)

std::string buffer(str.str());

std::cout<<buffer;

return 0;
}
 
J

Jon Bell

Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'

Sure, use stream manipulators just like you would if you were writing to
cout or a file. I'm not completely "up" on C-style formatting so I'm not
sure exactly what format you want, but this might come close:

str << "text " << fixed << setprecision(2) << value << " text";
 
W

Woodster

Thanks for the prompt response from all who replied. This will get me
going for what I am trying to do.

Regards

Sean Hannan
 
J

Jon Bell

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
// C-style method

double value = 10.5;
char buf[80];
sprintf (buf, "text %.2f text\n", value);
cout << buf;

// C++-style method

ostringstream bufstream;
bufstream << "text "
<< fixed << setprecision(2) << value
<< " text\n";
cout << bufstream.str();

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top