0

Couldn't find an answer for following question:

I'm new to C++ and am doing some tests now. I'm using std::fixed and std::setprecision functions to get a decimal number with 5 zeros. This works using cout, but i have no idea how to assigning the results to a variable instead of printing them using cout.

//Works
int fetchJD(std::string str) {
    std::cout << std::fixed << std::setprecision(0) << str;
    return 0;
}

I tried things like using sstream but whenever i try to assign the results to another string so i can return the results, lets the compiler fails.

// Not working  
int fetchJD(std::string str)
{
    std::stringstream ss;
    string temp;
    ss << std::fixed << std::setprecision(0) << str;
    temp = ss.str();

    return temp;
}

Which results in the compiler as error:

In function ‘int fetchJD(std::__cxx11::string)’:
astroapp.cpp:43:12: error: cannot convert ‘std::__cxx11::string {aka 
std::__cxx11::basic_string<char>}’ to ‘int’ in return
return temp;

So how can i pass the results from std::fixed << std::setprecision(0) << str; into another string?

Esocoder
  • 1,032
  • 1
  • 19
  • 40
  • 2
    `fetchJD` returns `int` not `std::string` – Jans Jan 05 '19 at 17:32
  • 1
    What is your goal? – Fureeish Jan 05 '19 at 17:33
  • 4
    You cannot assign a numeric value with a particular precision to a variable and have it keep the precision. –  Jan 05 '19 at 17:33
  • This code doesn't match the words. It writes the content of a string to the console; `std::fixed` and `std::setprecision(0)` have no effect on insertion of strings. – Pete Becker Jan 05 '19 at 17:45
  • I am trying to fetch a decimal number from an argument from the commandline which is decimal number like 2445874.82708, but i need to convert this number to a double or float which need to returns the exact same value – Esocoder Jan 05 '19 at 17:58
  • 1
    See my answer, there you have the functions that convert a string to a number. – Matthieu Brucher Jan 05 '19 at 18:01
  • ***I am trying to fetch a decimal number from an argument from the commandline which is decimal number like 2445874.82708, but i need to convert this number to a double or float which need to returns the exact same value*** You can't do this. That is not how the floating point hardware encodes the number. – drescherjm Jan 05 '19 at 19:56
  • @HermesTrismegistus assign the command line argument to a `std::istringstream` and then use `operator>>` to read it into a `float`/`double` variable. Or use `std::stof()` or `std::stod()` – Remy Lebeau Jan 05 '19 at 21:45

2 Answers2

2

You still need to convert the result back to an int (but that will never change the way the integer is displayed).

return std::stoi(ss.str());

This gives you an int, a simple int, no precision, nothing. All the processing of changing the precision and the fixed won't change anything, as you have a string at the beginning, which is not changed by these modifiers.

So basically you are not doing anything with your function.

If you want to convert a string to a value, use stoi, stod...

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • You are correct, i am missing the point. I should ask a total different question. But this is still the right answer, since i asked how to return the results, and this works. Which still leaves me with another question how to keep the number 2445874.82708 intact. – Esocoder Jan 05 '19 at 18:13
  • 1
    That would probably be a duplicate of https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Matthieu Brucher Jan 05 '19 at 18:16
2

You need to distinguish between what value you have in a variable e.g. double somevalue = 1.0; with what is displayed on the screen to the user using cout. The stream instructions setprecision and setfixed convert a double/float value to a certain output format. If you want to convert what is output to a string, then use stringstream instead of cout.

std::cout << std::fixed << std::setprecision(0) << somevalue;

vs

std::stringstream ss;
ss << std::fixed << std::setprecision(0) << somevalue;

then you have the content in the string ss.str()

AndersK
  • 35,813
  • 6
  • 60
  • 86