Difference between revisions of "cpp/string/basic string/to wstring"
From cppreference.com
< cpp | string | basic string
m (langlinks) |
(adapted example from to_string to show more) |
||
Line 57: | Line 57: | ||
{{example| | {{example| | ||
| code= | | code= | ||
− | |||
#include <iostream> | #include <iostream> | ||
+ | #include <string> | ||
− | + | template <typename T> | |
− | + | void print(T f) | |
− | std:: | + | { |
− | + | std::wcout << "std::cout: " << f << '\n' | |
+ | << "to_string: " << std::to_wstring(f) << "\n\n"; | ||
} | } | ||
− | | output= 23.430000 | + | |
+ | int main() | ||
+ | { | ||
+ | print(23.43); | ||
+ | print(1e-9); | ||
+ | print(1e40); | ||
+ | print(1e-40); | ||
+ | print(123456789.0); | ||
+ | } | ||
+ | | output= | ||
+ | std::cout: 23.43 | ||
+ | to_string: 23.430000 | ||
+ | |||
+ | std::cout: 1e-09 | ||
+ | to_string: 0.000000 | ||
+ | |||
+ | std::cout: 1e+40 | ||
+ | to_string: 10000000000000000303786028427003666890752.000000 | ||
+ | |||
+ | std::cout: 1e-40 | ||
+ | to_string: 0.000000 | ||
+ | |||
+ | std::cout: 1.23457e+08 | ||
+ | to_string: 123456789.000000 | ||
}} | }} | ||
Revision as of 04:36, 8 July 2021
Defined in header <string>
|
||
std::wstring to_wstring( int value ); |
(1) | (since C++11) |
std::wstring to_wstring( long value ); |
(2) | (since C++11) |
std::wstring to_wstring( long long value ); |
(3) | (since C++11) |
std::wstring to_wstring( unsigned value ); |
(4) | (since C++11) |
std::wstring to_wstring( unsigned long value ); |
(5) | (since C++11) |
std::wstring to_wstring( unsigned long long value ); |
(6) | (since C++11) |
std::wstring to_wstring( float value ); |
(7) | (since C++11) |
std::wstring to_wstring( double value ); |
(8) | (since C++11) |
std::wstring to_wstring( long double value ); |
(9) | (since C++11) |
Converts a numeric value to std::wstring.
1) Converts a signed decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%d", value) would produce for sufficiently large
buf
.2) Converts a signed decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%ld", value) would produce for sufficiently large
buf
.3) Converts a signed decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%lld", value) would produce for sufficiently large
buf
.4) Converts an unsigned decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%u", value) would produce for sufficiently large
buf
.5) Converts an unsigned decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%lu", value) would produce for sufficiently large
buf
.6) Converts an unsigned decimal integer to a wide string with the same content as what std::swprintf(buf, sz, L"%llu", value) would produce for sufficiently large
buf
.7,8) Converts a floating point value to a wide string with the same content as what std::swprintf(buf, sz, L"%f", value) would produce for sufficiently large
buf
.9) Converts a floating point value to a wide string with the same content as what std::swprintf(buf, sz, L"%Lf", value) would produce for sufficiently large
buf
.Contents |
Parameters
value | - | a numeric value to convert |
Return value
a wide string holding the converted value
Exceptions
May throw std::bad_alloc from the std::wstring constructor.
Example
Run this code
#include <iostream> #include <string> template <typename T> void print(T f) { std::wcout << "std::cout: " << f << '\n' << "to_string: " << std::to_wstring(f) << "\n\n"; } int main() { print(23.43); print(1e-9); print(1e40); print(1e-40); print(123456789.0); }
Output:
std::cout: 23.43 to_string: 23.430000 std::cout: 1e-09 to_string: 0.000000 std::cout: 1e+40 to_string: 10000000000000000303786028427003666890752.000000 std::cout: 1e-40 to_string: 0.000000 std::cout: 1.23457e+08 to_string: 123456789.000000
See also
(C++11) |
converts an integral or floating-point value to string (function) |