473,761 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10053
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<<buff er;

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<<buff er;

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.alcate l.be>,
Sa************* *******@adcc.al catel.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.alcate l.be>,
Sa************* *******@adcc.al catel.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::setprecisi on:

#include <iomanip>
#include <sstream>

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

str << std::setprecisi on(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.alcate l.be>,
Sa************* *******@adcc.al catel.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::setprec ision(5)<<value <<" text"; //--> value will have
exactly five digits( before decimal point + after decimal point)

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

std::cout<<buff er;

return 0;
}


Sean Hannan


Jul 19 '05 #6
In article <MP************ ************@ne ws.westnet.com. au>,
Woodster <mi****@127.0.0 .1> wrote:
In article <3F************ ***@adcc.alcate l.be>,
Sa************ ********@adcc.a lcatel.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*******@pres by.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**********@j tbell.presby.ed u>, jt*******@presb y.edu
says...
In article <MP************ ************@ne ws.westnet.com. au>,
Woodster <mi****@127.0.0 .1> wrote:
In article <3F************ ***@adcc.alcate l.be>,
Sa************ ********@adcc.a lcatel.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************ ***********@new s.westnet.com.a u>,
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*******@pres by.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
13093
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 classes used in this program. All strings in this program are of the Borland AnsiString class. I would like to convert them over to use std::string. In order to keep from breaking the original program, I was hoping to use std::string everywhere...
2
2139
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
4814
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
6
11509
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
7
4866
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 main() { std::string s = "hi/hello/now";
84
15893
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
2901
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
14
24279
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 MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
4
6509
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 converting to a char buf and then putting it back in std::string - is anyone aware of any other way? Thanks
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.