Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting to wstring simpler way of doing it

Tags:

c++

wstring

I need to format a double. I use the code bellow

wstring to_wstring1(double f){
  wchar_t wc[0x40];swprintf(wc,countof(wc),L"%E",f);return wstring(wc);
}

Is there a way to do it that avoids using the buffer wc? I am thinking to allocate a wstring with the size of the buffer and write there and after resize the string up to the final 0. Is there a simpler way?

like image 871
George Kourtis Avatar asked Dec 07 '25 05:12

George Kourtis


2 Answers

You can use std::to_wstring(double).

Note: It does require C++11

like image 128
ScarletAmaranth Avatar answered Dec 09 '25 21:12

ScarletAmaranth


Something like this?

#include <iostream>
#include <sstream>

int main() {
    std::wostringstream w;
    w << 1.0;;
    std::wcout << w.str() << std::endl;
}

Note: It does not require C++11


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!