C++11 std::to_string(double) - No trailing zeros

C++StlC++11

C++ Problem Overview


Today I tried out some new functions of the C++11 STL and encountered std::to_string.

Lovely, lovely set of functions. Creating a stringstream object for just one double-to-string conversion always seemed overkill to me, so I'm glad we can now do something like this:

std::cout << std::to_string(0.33) << std::endl;

The result?

0.330000

I'm not entirely content with that. Is there a way to tell std::to_string to leave out the trailing zeros? I searched the internet, but as far as I can see the function takes only one argument (the value to be converted). Back in 'the old days' with stringstreams, you could set the width of the stream, but I'd rather not convert back.

Anyone encountered this problem before/has a solution? Some StackOverflow searches yielded nothing.

(A C++11 STL reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string)

C++ Solutions


Solution 1 - C++

If all you want to do is remove trailing zeros, well, that's easy.

std::string str = std::to_string (f);
str.erase ( str.find_last_not_of('0') + 1, std::string::npos );

Solution 2 - C++

The C++11 Standard explicitely says (21.5/7):

> Returns: Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size

for the functions declared in this order:

string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);

Thus, you cannot control the formatting of the resulting string.

Solution 3 - C++

std::to_string gives you no control over the format; you get the same result as sprintf with the appropriate format specifier for the type ("%f" in this case).

If you need more flexibility, then you will need a more flexible formatter - such as std::stringstream.

Solution 4 - C++

To leave out the trailing zeros:

std::ostringstream oss;
oss << std::setprecision(8) << std::noshowpoint << double_value;
std::string str = oss.str();

Note: #include <sstream> and #include <iomanip>

Solution 5 - C++

std::to_string(double) is defined by the standard to just return the same sequence of characters that would be generated by sprintf(buf, "%f", value). No more, no less, especially no way to tweak the format specifier. So no, there is nothing you can do.

Solution 6 - C++

With boost::to_string you cannot control the format either but it will output something closer to what you see in the screen. Same with std::lexical_cast<std::string>.

For a function-like operation with format control, use str(boost::format("...format...")% 0.33).

https://stackoverflow.com/questions/29399064/whats-the-difference-between-stdto-string-boostto-string-and-boostlexic

Solution 7 - C++

A varied solution to the problem since to_string doesn't work. "The Magic Formula" - my CS2400 teacher

std::cout.setf(ios::fixed);
std::cout.setf(ios::showpoint);
std::cout.precision(2);

const double x = 0.33, y = 42.3748;
std::cout << "$" << x << std::endl;
std::cout << "$" << y << std::endl;

Outputs:

$0.33
$42.37

any following output you do with decimal numbers will be set as so.

you can always change the setf and precision as you see fit.

Solution 8 - C++

Create a custom convert function, remove the tailing zeros if necessary.

//! 2018.05.14 13:19:20 CST
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

//! Create a custom convert function, remove the tailing zeros if necessary.  
template<typename T>
std::string tostring(const T &n) {
    std::ostringstream oss;
    oss << n;
    string s =  oss.str();
    int dotpos = s.find_first_of('.');
    if(dotpos!=std::string::npos){
        int ipos = s.size()-1;
        while(s[ipos]=='0' && ipos>dotpos){
            --ipos;
        }
        s.erase ( ipos + 1, std::string::npos );
    }
    return s;
}

int main(){
    std::cout<< tostring(1230)<<endl;
    std::cout<< tostring(12.30)<<endl;
}

The input numbers :

1230
12.30

Compile with -std=c++11, then the result:

1230
12.3

Solution 9 - C++

Write a generic helper function, such as below, which you can repurpose for rounding needs within your C++ project.

inline static const string roundDouble(const double input, const int decimal_places)
{
	ostringstream str;
	str << fixed << setprecision(decimal_places);
	str << input;
	return str.str();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStijn FrishertView Question on Stackoverflow
Solution 1 - C++Marshall ClowView Answer on Stackoverflow
Solution 2 - C++rubenvbView Answer on Stackoverflow
Solution 3 - C++Mike SeymourView Answer on Stackoverflow
Solution 4 - C++Ivan Folgueira BandeView Answer on Stackoverflow
Solution 5 - C++Arne MertzView Answer on Stackoverflow
Solution 6 - C++alfCView Answer on Stackoverflow
Solution 7 - C++StackAttackView Answer on Stackoverflow
Solution 8 - C++Kinght 金View Answer on Stackoverflow
Solution 9 - C++tleatView Answer on Stackoverflow