function
<cstdlib>
strtoffloat strtof (const char* str, char** endptr);
Convert string to float
Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as afloat
. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.
The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. A pointer to the rest of the string after the last valid character is stored in the object pointed by endptr.
A valid floating point number for strtof using the "C"
locale is formed by an optional sign character (+
or -
), followed by one of:
.
), optionally followed by an exponent part (an e
or E
character followed by an optional sign and a sequence of digits).0x
or 0X
prefix, then a sequence of hexadecimal digits (as in isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p
or P
character followed by an optional sign and a sequence of hexadecimal digits).INF
or INFINITY
(ignoring case).NAN
or NAN
sequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_
).0.0F
.
char*
, whose value is set by the function to the next character in str after the numerical value.
float
.
0.0F
).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* strtof example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtof */
int main ()
{
char szOrbits[] = "686.97 365.24";
char* pEnd;
float f1, f2;
f1 = strtof (szOrbits, &pEnd);
f2 = strtof (pEnd, NULL);
printf ("One martian year takes %.2f Earth years.\n", f1/f2);
return 0;
}
One martian year takes 1.88 Earth years.
If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4