I have a double
in format xxxxx.yyyy
, for example 0.001500
.
I would like to convert it into wstring
, formatted with scientific notation. This is the result I want: 0.15e-2
.
I am not that experienced with C++, so I checked std::wstring
reference and haven't found any member function that can do this.
I have found similar threads here on Stack Overflow but I just do not know how to apply those answers to solve my problem especially as they do not use wchar
.
I have tried to solve this myself:
// get the value from database as double
double d = // this would give 0.5
// I do not know how determine proper size to hold the converted number
// so I hardcoded 4 here, so I can provide an example that demonstrates the issue
int len = 3 + 1; // + 1 for terminating null character
wchar_t *txt = new wchar_t[3 + 1];
memset(txt, L'\0', sizeof(txt));
swprintf_s(txt, 4, L"%e", d);
delete[] txt;
I just do not know how to allocate big enough buffer to hold the result of the conversion. Every time I get buffer overflow, and all the answers here from similar thread estimate the size. I really would like to avoid this type of introducing "magic" numbers.
I do not know how to use stringstream
either, because those answers did not convert double
into wstring
with scientific notation.
All I want is to convert double
into wstring
, with resulting wstring
being formatted with scientific notation.