Convert large double to scientific notation for use as string? [duplicate] - c++

This question already has answers here:
C++: Converting a double/float to string, preserve scientific notation and precision
(2 answers)
Closed 5 years ago.
This question is very clearly in no way an "exact duplicate" of the marked question, and that post does not address my goal.
If I have:
double yuge = 1e16;
And I try to add it to a string like this:
std::string boogers = std::to_string (yuge) + ".csv";
I get a file name of 100000000000000000.csv.
I want a nice compact version like 1e16.csv.
As you can see, I would like to use it as a file name, so output methods arent helpful. Halp! Thanks.

you can use std::stringstream to construct the string instead of + operator.
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
int main()
{
double youge = 1e16;
std::stringstream ss;
ss<<youge<<".csv";
std::string filename = ss.str(); // filename = 1e+16.csv
filename.erase(std::remove(filename.begin(), filename.end(), '+'), filename.end());// removing the '+' sign
std::cout<<filename; // 1e16.csv
}

Related

Replacing all instances of one ascii character with another in c++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace all occurrences of a character in string?
E.g, I have a string, "Hello World" and I want to replace all the "l"s with "1"s. How would I do this? I am new to c++. Most of my background is in Python in which you could just use the .replace method.
Use std::replace.
#include <string>
#include <algorithm>
#include <iostream>
int main() {
std::string str = "Hello World";
std::replace(str.begin(),str.end(),'l','1');
std::cout << str; //He11o Wor1d
}

How to convert Integer to string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.

Convert a Long in a String C++? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ long to string
Easiest way to convert int to string in C++
I am use to Java where I could just use .toString() on almost anything but I am trying a few problems in C++
I can not figure out how to make a long value into a string.
You can either use a string stream:
#include <sstream>
#include <string>
long x;
// ...
std::ostringstream ss;
ss << x;
std::string result = ss.str();
Or you can use Boost's lexical_cast:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(x);
I think it's a common opinion that this aspect of the language isn't quite as elegant as it could be.
In the new C++11, things are a bit easier, and you can use the std::to_string() function:
#include <string>
std::string s = std::to_string(x);
#include <string>
#include <sstream>
std::ostringstream ss;
long i = 10;
ss << i;
std::string str = ss.str();
You can use a stringstream.
std::ostringstream ss;
ss << aLongNumber;
ss.str()
You use operator << like iostream cout and cin. And you use str() method to get the string.

how to tokenize a string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string
thanks
#include <sstream>
#include <vector>
#include <string>
int main() {
std::string str("abc\tdef");
char split_char = '\t';
std::istringstream split(str);
std::vector<std::string> token;
for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}
Why can't you use C standard library?
Variant 1.
Use std::string::c_str() function to convert a std::string to a C-string (char *)
Variant 2.
Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.

How do you convert a C++ string to an int? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to parse a string to an int in C++?
How do you convert a C++ string to an int?
Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).
Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.
#include <sstream>
// st is input string
int result;
stringstream(st) >> result;
Use the C++ streams.
std::string plop("123");
std::stringstream str(plop);
int x;
str >> x;
/* Lets not forget to error checking */
if (!str)
{
// The conversion failed.
// Need to do something here.
// Maybe throw an exception
}
PS. This basic principle is how the boost library lexical_cast<> works.
My favorite method is the boost lexical_cast<>
#include <boost/lexical_cast.hpp>
int x = boost::lexical_cast<int>("123");
It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).
I have used something like the following in C++ code before:
#include <sstream>
int main()
{
char* str = "1234";
std::stringstream s_str( str );
int i;
s_str >> i;
}
C++ FAQ Lite
[39.2] How do I convert a std::string to a number?
https://isocpp.org/wiki/faq/misc-technical-issues#convert-string-to-num
Let me add my vote for boost::lexical_cast
#include <boost/lexical_cast.hpp>
int val = boost::lexical_cast<int>(strval) ;
It throws bad_lexical_cast on error.
Use atoi
Perhaps I am misunderstanding the question, by why exactly would you not want to use atoi? I see no point in reinventing the wheel.
Am I just missing the point here?
in "stdapi.h"
StrToInt
This function tells you the result, and how many characters participated in the conversion.