-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_string.cpp
34 lines (27 loc) · 980 Bytes
/
to_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "boost/lexical_cast.hpp"
#if __cpp_lib_format
# include <format> // c++20 only
using std::format;
#else
# include <fmt/core.h>
using ::fmt::format;
#endif
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
int main()
{
double maxDouble = std::numeric_limits<double>::max();
std::string str(std::to_string(maxDouble));
std::cout << "std::to_string(" << std::setprecision(16) << maxDouble << ") == " << str
<< std::endl;
std::cout << "boost::lexical_cast<std::string>(" << maxDouble << ") == " << std::setprecision(16)
<< boost::lexical_cast<std::string>(maxDouble) << std::endl;
double back_stod = std::stod(str);
double back_cast = boost::lexical_cast<double>(str);
std::cout << "boost::lexical_cast<double>(" << back_cast << ") == " << back_stod << std::endl;
std::string s = format("The format::answer is {}.", maxDouble);
std::cout << s << std::endl;
return 0;
}