strtod()
strtod double strtod (const char* str, char** endptr);
Convert string to double
Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as a double
. 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.
C90 (C++98)
A valid floating point number for strtod using the "C"
locale is formed by an optional sign character ( +
or -
), followed by a sequence of digits, optionally containing a decimal-point character ( .
), optionally followed by an exponent part (an e
or E
character followed by an optional sign and a sequence of digits).
If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just described, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed and the function returns a zero value.
Parameters
str
C-string beginning with the representation of a floating-point number.
endptr
Reference to an already allocated object of type char*
, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a
null pointer, in which case it is not used.
Return Value
On success, the function returns the converted floating point number as a value of type double
.
If no valid conversion could be performed, the function returns zero ( 0.0
).
If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VAL is returned, and errno is set to ERANGE.
C90 (C++98)
If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number and sets errno to ERANGE.
Example
/* strtod example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtod */
int main ()
{
char szOrbits[] = "365.24 29.53";
char* pEnd;
double d1, d2;
d1 = strtod (szOrbits, &pEnd);
d2 = strtod (pEnd, NULL);
printf ("The moon completes %.2f orbits per Earth year.
", d1/d2);
return 0;
}
Output:
The moon completes 12.37 orbits per Earth year.
double strtod (const char* str, char** endptr);
/* strtod example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtod */
int main ()
{
char szOrbits[] = "365.24 29.53";
char* pEnd;
double d1, d2;
d1 = strtod (szOrbits, &pEnd);
d2 = strtod (pEnd, NULL);
printf ("The moon completes %.2f orbits per Earth year.
", d1/d2);
return 0;
}
The moon completes 12.37 orbits per Earth year.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.