C99中字符串转浮点型strtof strtod

本文详细介绍了C99标准中的`strtof`, `strtod`, `strtold`等函数,用于将字符串转换为浮点数。这些函数在`<stdlib.h>`头文件中定义,它们解析字符串内容并返回浮点数值。文章通过示例展示了如何使用`strtof`和`atof`进行转换,并提供了错误处理和范围检查的说明。同时,还给出了计算火星年和地球年比例以及计算正弦值的代码示例。
摘要由CSDN通过智能技术生成

C99中字符串转浮点型strtof strtod

字符串转浮点型

在头文件<stdlib.h>中定义标准
float strtof(const char * restrict str,char ** restrict str_end);(自C99以来)
double strtod(const char * str,char ** str_end);(直到C99)
double strtod(const char * restrict str,char ** restrict str_end);(自C99以来)
long double strtold(const char * restrict str,char ** restrict str_end);(自C99以来)
double strtod(const char * restrict str,char ** restrict str_end);(自C99以来)
double atof (const char* str);
float strtof (const char* str, char** endptr);
double strtod (const char* str, char** endptr);
long double strtold (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 a float. 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:

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).

A 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 NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).

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 0.0F.

函数参数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 float.
If no valid conversion could be performed, the function returns zero (0.0F).
If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VALF is returned, and errno is set to ERANGE.
If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number (some library implementations may also set errno to ERANGE in this case).
返回值:
浮点值对应str的成功内容。 如果转换后的值超出相应返回类型的范围,则会发生范围错误,并返回HUGE_VAL,HUGE_VALF或HUGE_VALL。 如果不能执行转换,则返回0。

/* 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;
}
/* atof example: sine calculator */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atof */
#include <math.h>       /* sin */

int main ()
{
  double n,m;
  double pi=3.1415926535;
  char buffer[256];
  printf ("Enter degrees: ");
  fgets (buffer,256,stdin);
  n = atof (buffer);
  m = sin (n*pi/180);
  printf ("The sine of %f degrees is %f\n" , n, m);
  return 0;
}

资料来源:
http://www32.cplusplus.com/reference/cstdlib/strtof/

https://cloud.tencent.com/developer/section/1009642

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值