Konwersja std::string na float

0

Witam,

dlaczego taki string ("0.000085") po przekonwertowaniu za pomocą std::stof wynosi 8.5e-05, a np. ("0.214334") zwraca poprawny wynik (0.214334).

Ideone: http://ideone.com/FwnjhD

kod:

 
    #include <iostream>
    #include <string>
     
    int main() 
    {
    	std::string f0 = "0.000085";
    	std::string f1 = "0.214334";
     
    	std::cout << std::stof(f0.c_str()) << std::endl;
    	std::cout << std::stof(f1.c_str()) << std::endl;
     
    	return 0;
    }
2
#include <iostream>
#include <string>

int main() 
{
	std::string f0 = "0.000085";
	std::string f1 = "0.214334";
	std::cout << std::fixed;
	std::cout << std::stof(f0) << std::endl;
	std::cout << std::stof(f1) << std::endl;
	return 0;
}

http://ideone.com/S4riGC

0.000085
0.214334

Zaznaczę jeszcze, że stof wcale nie zwraca liczb w naukowej notacji.

#include <iostream>
#include <string>
using namespace std;

int main() {
	string str = "0.000085";
	cout << str.c_str();
}

http://ideone.com/bsVoz9

0.000085

0

dzięki

1 użytkowników online, w tym zalogowanych: 0, gości: 1