473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question: std:string and float values??

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
Jul 19 '05 #1
8 10031
Woodster wrote:
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..



Thanks in advance

Sean Hannan


Jul 19 '05 #2
>

Sorry a small correction in the program

Use stringstream

#include <iostream>
#include <sstream>
#include <string> // I missed it last time...


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

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

std::string buffer(str.str()); //In my last post it was just string...not
std::string.

std::cout<<buffer;

return 0;
}

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


Thanks in advance

Sean Hannan


Jul 19 '05 #3
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
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
Jul 19 '05 #4
Woodster <mi****@127.0.0.1> writes:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
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'


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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #5
Woodster wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
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'


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;
}


Sean Hannan


Jul 19 '05 #6
In article <MP************************@news.westnet.com.au> ,
Woodster <mi****@127.0.0.1> wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
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'


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";

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #7
Thanks for the prompt response from all who replied. This will get me
going for what I am trying to do.

Regards

Sean Hannan

In article <bp**********@jtbell.presby.edu>, jt*******@presby.edu
says...
In article <MP************************@news.westnet.com.au> ,
Woodster <mi****@127.0.0.1> wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
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'


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";

Jul 19 '05 #8
In article <MP***********************@news.westnet.com.au>,
Woodster <mi****@127.0.0.1> wrote:

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;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Victor Hannak | last post by:
I am taking a program written in Borland C++ Builder 4 and converting the non-GUI related code to be generic c++ that can run anywhere. My main issue at this point is dealing with the string...
2
by: Aiscrim | last post by:
Hi everyone, what function can be used to convert a float to a std::string variable? Or, if such a function doesn't exist, how could I implement one by myself? Thanx, Ste
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
7
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
4
by: Ramesh | last post by:
Hi, I need to convert unsigned long data to a std::string, I googled a bit to see if the string class supports any methods to achieve this - but didnt find any, I think of using sprintf and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.