std::wcstof...td::wcstold(3)
π std::wcstof, std::wcstod, std::wcstold π
okay bestie, so this is from the C++ Standard Library manual β std::wcstof...td::wcstold(3) β and no, that truncated name isn't a typo on my end, that's literally how the page introduces itself. We're doing wide-string-to-float conversions today and honestly? iconic behavior.
β¨ NAME β¨
std::wcstof, std::wcstod, std::wcstold β literally the whole name is just std::wcstof,std::wcstod,std::wcstold. That's it. That's the name. The manpage said "let the functions speak for themselves" and I respect it π«Ά
πΈ SYNOPSIS πΈ
First things first, they live in the header <cwchar> (yes, the wide one, not the plain one, don't @ me).
float wcstof( const wchar_t* str, wchar_t** str_end );
(since C++11)
double wcstod( const wchar_t* str, wchar_t** str_end );
long double wcstold( const wchar_t* str, wchar_t** str_end );
(since C++11)
So basically: interprets a floating point value in a wide string pointed to by str. three flavors, three widths, one vibe π«
Here's the actual tea on how it parses, step by step:
- first it discards any whitespace characters (as determined by
std::iswspace) until the first non-whitespace character is found - then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value
and what counts as a "valid floating-point value"? glad you asked, there are FIVE whole categories, buckle up π
- decimal floating-point expression β it consists of these parts:
- (optional) plus or minus sign
- nonempty sequence of decimal digits optionally containing decimal-point character (as determined by the current C locale) (defines significand)
- (optional)
eorEfollowed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 10)
- hexadecimal floating-point expression β it consists of these parts:
- (optional) plus or minus sign
0xor0X- nonempty sequence of hexadecimal digits optionally containing a decimal-point character (as determined by the current C locale) (defines significand)
- (optional)
porPfollowed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 2) (since C++11)
- infinity expression β it consists of these parts:
- (optional) plus or minus sign
INForINFINITYignoring case
- not-a-number expression β it consists of these parts:
- (optional) plus or minus sign
NANorNAN(char_sequence)ignoring case of theNANpart.char_sequencecan only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value.
- any other expression that may be accepted by the currently installed C locale π
and then the functions set the pointer pointed to by str_end to point to the wide character past the last character interpreted. If str_end is a null pointer, it is ignored. no cap, that's the whole deal π
π¦ PARAMETERS π¦
strβ pointer to the null-terminated wide string to be interpretedstr_endβ pointer to a pointer to a wide character
two params, both doing exactly what they say on the label. we love a low-maintenance queen π
β RETURN VALUE β
Floating point value corresponding to the contents of str on success. slay.
but π if the converted value falls out of range of corresponding return type, a range error occurs and HUGE_VAL, HUGE_VALF or HUGE_VALL is returned. so like, dramatic much, but okay.
and if no conversion can be performed, 0 is returned. zero. nothing. silence. π
π§ EXAMPLE π§
// Run this code
#include <cerrno>
#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>
int main()
{
const wchar_t* p = L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz";
wchar_t* end;
std::wcout << "Parsing L\"" << p << "\":\n";
for (double f = std::wcstod(p, &end); p != end; f = std::wcstod(p, &end))
{
std::wcout << " '" << std::wstring(p, end-p) << "' -> ";
p = end;
if (errno == ERANGE)
{
std::wcout << "range error, got ";
errno = 0;
}
std::wcout << f << '\n';
}
if (std::setlocale(LC_NUMERIC, "de_DE.utf8"))
{
std::wcout << L"With de_DE.utf8 locale:\n";
std::wcout << L" '123.45' -> " << std::wcstod(L"123.45", 0) << L'\n';
std::wcout << L" '123,45' -> " << std::wcstod(L"123,45", 0) << L'\n';
}
}
and the output is giving exactly this π
Parsing L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz":
'111.11' -> 111.11
' -2.22' -> -2.22
' 0X1.BC70A3D70A3D7P+6' -> 111.11
' -Inf' -> -inf
' 1.18973e+4932' -> range error, got inf
With de_DE.utf8 locale:
'123.45' -> 123
'123,45' -> 123.45
that de_DE.utf8 bit at the end is the main character of the whole example btw β locale changes what counts as a decimal point and the output literally shifts. observe and learn π
π SEE ALSO π
strtofβ converts a byte string to a floating-point valuestrtodβ (function)strtoldβ the long double cousin, also a function- C documentation for
wcstofβ the origin story, iykyk
bestie reminder: thoseerrno == ERANGEchecks are not decoration. range errors are real and the returnedHUGE_VAL/HUGE_VALF/HUGE_VALLwon't flag themselves for you π
π that's the whole page, bestie π brought to you by the C++ Standard Library manual, sourced from http://cppreference.com, and dated 2024.06.10 β go forth and parse some wide strings, you absolute icon π¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms