Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/strtof"

From cppreference.com
< cpp‎ | string‎ | byte
(fix the example and its output)
m ({{c}}, headers sorted)
 
(12 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/title| strtof | strtod | strtold}}
+
{{cpp/title|strtof|strtod|strtold}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | cstdlib}}
+
{{dcl header|cstdlib}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl|since=c++11|
float      strtof( const char* str, char** str_end );          
+
float      strtof( const char* str, char** str_end );
 
}}
 
}}
{{ddcl list item |  
+
{{dcl|
 
double      strtod( const char* str, char** str_end );
 
double      strtod( const char* str, char** str_end );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl|since=c++11|
 
long double strtold( const char* str, char** str_end );
 
long double strtold( const char* str, char** str_end );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Interprets a floating point value in a byte string pointed to by {{tt|str}}.
+
Interprets a floating point value in a byte string pointed to by {{c|str}}.
  
 
{{cpp/string/cvt str2float}}
 
{{cpp/string/cvt str2float}}
  
The functions sets the pointer pointed to by {{tt|str_end}} to point to the character past the last character interpreted. If {{tt|str_end}} is {{c|NULL}}, it is ignored.
+
The functions sets the pointer pointed to by {{c|str_end}} to point to the character past the last character interpreted. If {{c|str_end}} is a null pointer, it is ignored.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | str | pointer to the null-terminated byte string to be interpreted}}
+
{{par|str|pointer to the null-terminated byte string to be interpreted}}
{{param list item | str_end | pointer to a pointer to character.}}
+
{{par|str_end|pointer to a pointer to character.}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Floating point value corresponding to the contents of {{tt|str}} on success. If the converted value falls out of range of corresponding return type, range error occurs and {{c|HUGE_VAL}}, {{c|HUGE_VALF}} or {{c|HUGE_VALL}} is returned. If no conversion can be performed, {{c|0}} is returned.
+
Floating point value corresponding to the contents of {{c|str}} on success. If the converted value falls out of range of corresponding return type, range error occurs and {{lc|HUGE_VAL}}, {{lc|HUGE_VALF}} or {{lc|HUGE_VALL}} is returned. If no conversion can be performed, {{c|0}} is returned and {{c|*str_end}} is set to {{c|str}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cerrno>
 +
#include <clocale>
 
#include <cstdlib>
 
#include <cstdlib>
#include <cstring>
 
 
#include <iostream>
 
#include <iostream>
+
#include <string>
 +
 
 
int main()
 
int main()
 
{
 
{
     std::cout << std::strtod("0.0000000123", nullptr) << "\n";
+
    const char* p = "111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz";
+
    char* end{};
     // the second arg of strto* is non-const. We must copy the string
+
     std::cout << "Parsing \"" << p << "\":\n";
    char src[] = "0.012  15e16 -0x1afp-2 inF Nan";
+
     errno = 0;
     char* c = src;
+
     for (double f = std::strtod(p, &end); p != end; f = std::strtod(p, &end))
      
+
     {
    std::cout << std::strtod(c, &c) << "\n";
+
        std::cout << "  '" << std::string(p, end - p) << "' -> ";
    std::cout << std::strtod(c, &c) << "\n";
+
        p = end;
     std::cout << std::strtod(c, &c) << "\n";
+
        if (errno == ERANGE)
    std::cout << std::strtod(c, &c) << "\n";
+
        {
    std::cout << std::strtod(c, &c) << "\n";
+
            std::cout << "range error, got ";
 +
            errno = 0;
 +
        }
 +
        std::cout << f << '\n';
 +
     }
 +
 
 +
    if (std::setlocale(LC_NUMERIC, "de_DE.utf8"))
 +
    {
 +
        std::cout << "With de_DE.utf8 locale:\n";
 +
        std::cout << "  '123.45' -> " << std::strtod("123.45", 0) << '\n';
 +
        std::cout << "  '123,45' -> " << std::strtod("123,45", 0) << '\n';
 +
    }
 
}
 
}
| output=
+
|p=true
1.23e-08
+
|output=
0.012
+
Parsing "111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz":
1.5e+17
+
  '111.11' -> 111.11
-107.75
+
  ' -2.22' -> -2.22
inf
+
  ' 0X1.BC70A3D70A3D7P+6' -> 111.11
nan
+
  ' -Inf' -> -inf
 +
  ' 1.18973e+4932' -> range error, got inf
 +
With de_DE.utf8 locale:
 +
  '123.45' -> 123
 +
  '123,45' -> 123.45
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/byte/dcl list atof}}
+
{{dsc inc|cpp/string/byte/dsc atof}}
{{dcl list see c | c/string/byte/strtof | strtof | strtod | strtold}}
+
{{dsc inc|cpp/string/wide/dsc wcstof}}
{{dcl list end}}
+
{{dsc inc|cpp/utility/dsc from_chars}}
 +
{{dsc see c|c/string/byte/strtof|strtof|strtod|strtold}}
 +
{{dsc end}}
  
[[de:cpp/string/byte/strtof]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/byte/strtof]]
+
[[fr:cpp/string/byte/strtof]]
+
[[it:cpp/string/byte/strtof]]
+
[[ja:cpp/string/byte/strtof]]
+
[[pt:cpp/string/byte/strtof]]
+
[[ru:cpp/string/byte/strtof]]
+
[[zh:cpp/string/byte/strtof]]
+

Latest revision as of 08:41, 6 June 2023

Defined in header <cstdlib>
float       strtof( const char* str, char** str_end );
(since C++11)
double      strtod( const char* str, char** str_end );
long double strtold( const char* str, char** str_end );
(since C++11)

Interprets a floating point value in a byte string pointed to by str.

Function discards any whitespace characters (as determined by std::isspace) until 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. The valid floating-point value can be one of the following:

  • decimal floating-point expression. It consists of the following 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) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 10)
  • hexadecimal floating-point expression. It consists of the following parts:
  • (optional) plus or minus sign
  • 0x or 0X
  • nonempty sequence of hexadecimal digits optionally containing a decimal-point character (as determined by the current C locale) (defines significand)
  • (optional) p or P followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 2)
  • infinity expression. It consists of the following parts:
  • (optional) plus or minus sign
  • INF or INFINITY ignoring case
  • not-a-number expression. It consists of the following parts:
  • (optional) plus or minus sign
  • NAN or NAN(char_sequence) ignoring case of the NAN part. char_sequence can only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value.
(since C++11)
  • any other expression that may be accepted by the currently installed C locale

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is a null pointer, it is ignored.

Contents

[edit] Parameters

str - pointer to the null-terminated byte string to be interpreted
str_end - pointer to a pointer to character.

[edit] Return value

Floating point value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs and HUGE_VAL, HUGE_VALF or HUGE_VALL is returned. If no conversion can be performed, 0 is returned and *str_end is set to str.

[edit] Example

#include <cerrno>
#include <clocale>
#include <cstdlib>
#include <iostream>
#include <string>
 
int main()
{
    const char* p = "111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz";
    char* end{};
    std::cout << "Parsing \"" << p << "\":\n";
    errno = 0;
    for (double f = std::strtod(p, &end); p != end; f = std::strtod(p, &end))
    {
        std::cout << "  '" << std::string(p, end - p) << "' -> ";
        p = end;
        if (errno == ERANGE)
        {
            std::cout << "range error, got ";
            errno = 0;
        }
        std::cout << f << '\n';
    }
 
    if (std::setlocale(LC_NUMERIC, "de_DE.utf8"))
    {
        std::cout << "With de_DE.utf8 locale:\n";
        std::cout << "  '123.45' -> " << std::strtod("123.45", 0) << '\n';
        std::cout << "  '123,45' -> " << std::strtod("123,45", 0) << '\n';
    }
}

Possible output:

Parsing "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

[edit] See also

converts a byte string to a floating point value
(function) [edit]
converts a wide string to a floating-point value
(function) [edit]
converts a character sequence to an integer or floating-point value
(function) [edit]
C documentation for strtof, strtod, strtold