Inny rodzaj konwersji dostarcza std::to_string(), który zapisuje liczbę w postaci stringu (nie ma flag formatujących). Więcej informacji: http://en.cppreference.com/w/cpp/string/basic_string/to_string.
Co ciekawe, do standardu C++ została dołożona z C także funkcja std::snprintf(), nie byłem świadomy, że jej jeszcze tam nie było. Więcej info: http://en.cppreference.com/w/cpp/io/c/fprintf.
#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
int main()
{
const int i = std::stoi("567");
std::cout << "int: " << i << std::endl;
const std::string s = std::to_string(783);
std::cout << "string: " << s << std::endl;
const double d = 9.8765E4;
constexpr int size_tab = 100;
char tab[size_tab];
std::snprintf(tab, size_tab, "%e", d);
const std::string stab(tab);
std::cout << "double: " << stab << std::endl;
std::stringstream strstream;
strstream << "double: " << std::scientific << d << std::endl;
std::cout << strstream.str();
return 0;
}
Wynik.int: 567 string: 783 double: 9.876500e+04 double: 9.876500e+04
Brak komentarzy:
Prześlij komentarz