C++ : Convert double to string and manage precision | scientific notation

In this article we will discuss different ways to convert double to String or char array with or without precision i.e.

double to String using C++11’s std::to_string

std::to_string is introduced in c++11.

Example to convert double to std::string using to_string is as follows,

double num = 6789898989.33999443;
//Converting using std::to_string
std::string str = std::to_string(num);

Output:

6789898989.339994

It has default precision of 6 digits and we cannot change it. If you want customised precision then you should use stringstream.

double to String using ostringstream

Header file Required

#include <sstream>

Example to convert double to std::string using ostringstream is as follows,

// Create an output string stream
std::ostringstream streamObj;
//Add double to stream
streamObj << num;
// Get string from output string stream
std::string strObj = streamObj.str();

Output

6.7899e+09

For Big Numbers ostringstream will automatically convert it to scientific notation.

double to String with scientific notation using ostringstream

Add std::fixed to stream i.e.

// Set Fixed -Point Notation
streamObj2 << std::fixed;

It will force the ostringstream to convert double using fixed-point notation instead of scientific one. For example,

// Create an output string stream
std::ostringstream streamObj2;

// Set Fixed -Point Notation
streamObj2 << std::fixed;

//Add double to stream
streamObj2 << num;
// Get string from output string stream
std::string strObj2 = streamObj2.str();

Output

6789898989.339994

By default, precision is up to max 6 in stringstream’s double to string conversion.

double to String with Custom Precision using ostringstream

Header file Required

#include <iomanip>

To convert Double to String with Custom Precision set precision in stringstream i.e.

Let’s set precision to 2 i.e.

// Set precision to 2 digits
streamObj << std::setprecision(2);

Check this,

// Create an output string stream
std::ostringstream streamObj3;

// Set Fixed -Point Notation
streamObj3 << std::fixed;

// Set precision to 2 digits
streamObj3 << std::setprecision(2);

//Add double to stream
streamObj3 << num;

// Get string from output string stream
std::string strObj3 = streamObj3.str();

Output:

6789898989.34

double to String / Char array using C’s snprintf

char buffer[32];
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%g", num);

std::string strObj4(buffer);

Output

6.7899e+09

double to String boost’s lexical_cast

Header file Required

#include <boost/lexical_cast.hpp>

Example,

std::string strObj5 = "0.0";
try
{
	strObj5 = boost::lexical_cast<std::string>(num);
}
catch (boost::bad_lexical_cast const& e)
{
	std::cout << "Error: " << e.what() << "\n";
}

Output

6789898989.3399944

Complete example is as follows,

#include <string>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <iomanip>

int main()
{
	double num = 6789898989.33999443;

	std::cout << "Converting using std::to_string" << std::endl;

	std::string str = std::to_string(num);

	std::cout << str << std::endl;

	/****************************************/

	std::cout << "Converting double to string using ostringstream" << std::endl;
	// Create an output string stream
	std::ostringstream streamObj;
	//Add double to stream
	streamObj << num;
	// Get string from output string stream
	std::string strObj = streamObj.str();

	std::cout << strObj << std::endl;

	/****************************************/

	std::cout<< "Converting double to string without scientific Notation using ostringstream" << std::endl;
	// Create an output string stream
	std::ostringstream streamObj2;

	// Set Fixed -Point Notation
	streamObj2 << std::fixed;

	//Add double to stream
	streamObj2 << num;
	// Get string from output string stream
	std::string strObj2 = streamObj2.str();

	std::cout << strObj2 << std::endl;

	/****************************************/

	std::cout<< "Converting double to string with custom precision 2"<< std::endl;
	// Create an output string stream
	std::ostringstream streamObj3;

	// Set Fixed -Point Notation
	streamObj3 << std::fixed;

	// Set precision to 2 digits
	streamObj3 << std::setprecision(2);

	//Add double to stream
	streamObj3 << num;

	// Get string from output string stream
	std::string strObj3 = streamObj3.str();

	std::cout << strObj3 << std::endl;

	/****************************************/

	std::cout << "Converting double to string with snprintf" << std::endl;

	char buffer[32];
	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%g", num);

	std::string strObj4(buffer);

	std::cout << strObj4 << std::endl;

	/****************************************/
	std::cout << "Converting double to string with Boost's lexical_cast"<< std::endl;

	std::string strObj5 = "0.0";
	try
	{
		strObj5 = boost::lexical_cast<std::string>(num);
	}
	catch (boost::bad_lexical_cast const& e)
	{
		std::cout << "Error: " << e.what() << "\n";
	}
	std::cout << strObj5 << std::endl;

}

Output:

Converting using std::to_string
6789898989.339994
Converting double to string using ostringstream
6.7899e+09
Converting double to string without scientific Notation using ostringstream
6789898989.339994
Converting double to string with custom precision 2
6789898989.34
Converting double to string with snprintf
6.7899e+09
Converting double to string with Boost's lexical_cast
6789898989.3399944

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top