0

Anyone can explain?, why a string change value when I convert a floating number to a string .its like that when I convert a floating number (34.567) to a string. it converts as "34.5679987", something like that. How I can get actual string;

#include <bits/stdc++.h>

using namespace std;

string find_floating(string str) {
  int find = str.find(".");
  if (find == -1) {
    return "00";
  } else {

    str = str.substr(find + 1);
  }

  return str;
}

main() {

  float num;
  cout << "enter number : ";
  cin >> num;

  string str = to_string(num);

  cout << str << endl;
  cout << typeid(str).name() << endl;

  cout << find_floating(str);

}
palkenas
  • 19
  • 4
  • Also refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of relate SO posts. Also take a [SO tour](https://stackoverflow.com/tour). – Jason Aug 06 '22 at 15:00
  • `34.5679987` or `34.5669987`? – Martin Rosenau Aug 06 '22 at 15:01
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Aug 06 '22 at 16:07

0 Answers0