arqam arqam - 1 month ago 7
C++ Question

C++ : Removing scientific notation from number while converting to string

I saw all the examples saying to use

fixed
,
precision
, etc but all those are when people wanted to print the value.

What I am trying to do is convert my double value from number to string and I don't need the scientific notations.

My numbers example :
6.61914e+6


So how can it be done in a simple way, when most of my number are not very long.

Answer Source

from std::to_string(double) description:

As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits.

string s = std::to_string(6.61914e+6);

If this number of digits after decimal point is not enough you can use std::stringstream:

std::ostringstream str;
str << std::fixed << std::setprecision(digits) << 6.61914e+6;
std::string s = str.str();