0

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.

arqam
  • 3,582
  • 5
  • 34
  • 69
  • @tobi303 tried with a few examples and most of them are very complex and used basically for large numbers. My most numbers are not even very long and I just need to remove that e. – arqam Aug 22 '17 at 12:55
  • [C++11 std::to_string(double) - No trailing zeros](https://stackoverflow.com/q/13686482/669576) – 001 Aug 22 '17 at 13:08

3 Answers3

3

You can use the std::to_string function for the task:

double x = 6.61914e+6;
auto mystr = std::to_string(x);
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Ron
  • 14,674
  • 4
  • 34
  • 47
  • 6619136.000000 But how to remove the trailing zeroes. Original number didn't even have those zeroes. – arqam Aug 22 '17 at 12:58
  • 1
    `6.61913E+6` has the value `6619130` (at least, within limits of finite floating point precision). The trailing zero is necessary, otherwise the value is not represented correctly in the string. If you want to remove the zeros after the `'.'` it is easy to parse and remove them. – Peter Aug 22 '17 at 13:02
  • @Peter Yeah, but if we are converting to a string in most cases we might not need the zeroes after the decimal. So that adds one more step of removing the zeroes after the decimal point. – arqam Aug 22 '17 at 13:03
3

Quote 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.

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

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

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

If you want to remove trailing zeroes (according to Robinson's comment and answer to this question):

s.erase(s.find_last_not_of('0') + 1, std::string::npos);  
DAle
  • 8,990
  • 2
  • 26
  • 45
  • `6619136.000000` But do we have to again do string manipulation to remove the zeroes, as they were not there originally and there are not needed. – arqam Aug 22 '17 at 12:59
  • You can remove them with s.erase(s.find_last_not_of('0') + 1,std::string::npos ); You're right though, there should be something in iomanip to tell stringstream not to do that. IDK of anything. – Robinson Aug 22 '17 at 13:09
2

you can use std::stringstream with what you already know - std::fixed and std::setprecision:

#include <iostream>
#include <sstream>
#include <iomanip>

int main()
{
    std::stringstream ss;
    ss << std::fixed << std::setprecision(0) << 6.61914e+6;
    std::cout << ss.str() << "\n";
}

output:

6619140

Coliru

As correctly commented, this is not good as you need to know the precision

The only flexible way I can see is:

#include <iostream>
#include <sstream>
#include <iomanip>

int main()
{
    std::stringstream ss;
    ss << std::fixed << 6.61914e+6;
    std::string s = ss.str();
    s.erase(s.find_last_not_of('0') + 1, std::string::npos );
    std::cout << s << "\n";    
}
Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112