C语言 如何将字符串转换为浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7951019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string to float?
提问by abubacker
#include<stdio.h>
#include<string.h>
int main()
{
char s[100] ="4.0800" ;
printf("float value : %4.8f\n" ,(float) atoll(s));
return 0;
}
I expect the output should be 4.08000000whereas I got only 4.00000000.
我希望输出应该是,4.08000000而我只有4.00000000.
Is there any way to get the numbers after the dot?
有没有办法得到点后的数字?
回答by Mysticial
Use atof()or strtof()* instead:
使用atof()或strtof()* 代替:
printf("float value : %4.8f\n" ,atof(s));
printf("float value : %4.8f\n" ,strtof(s, NULL));
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/strtof/
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/strtof/
atoll()is meant for integers.atof()/strtof()is for floats.
atoll()用于整数。atof()/strtof()用于浮动。
The reason why you only get 4.00with atoll()is because it stops parsing when it finds the first non-digit.
你只得到4.00with的原因atoll()是因为它在找到第一个非数字时停止解析。
*Note that strtof()requires C99 or C++11.
*请注意,strtof()需要 C99 或 C++11。
回答by sam hocevar
Unfortunately, there is no way to do this easily. Every solution has its drawbacks.
不幸的是,没有办法轻松做到这一点。每个解决方案都有其缺点。
Use
atof()orstrtof()directly: this is what most people will tell you to do and it will work most of the time. However, if the program sets a locale or it uses a library that sets the locale (for instance, a graphics library that displays localised menus) and the user has their locale set to a language where the decimal separator is not.(such asfr_FRwhere the separator is,) these functions will stop parsing at the.and you will stil get4.0.Use
atof()orstrtof()but change the locale; it's a matter of callingsetlocale(LC_ALL|~LC_NUMERIC, "");before any call toatof()or the likes. The problem withsetlocaleis that it will be global to the process and you might interfer with the rest of the program. Note that you might query the current locale withsetlocale()and restore it after you're done.Write your own float parsing routine. This might be quite quick if you do not need advanced features such as exponent parsing or hexadecimal floats.
直接使用
atof()或strtof()直接使用:这是大多数人会告诉您这样做的,并且在大多数情况下都会起作用。然而,如果程序设置一个区域或它使用的库,设定区域(例如,图形库,显示局部菜单),并且用户具有他们的区域设置为一语言,其中小数分隔符不是.(如fr_FR其中分隔符是,) 这些函数将在 处停止解析,.您仍然会得到4.0.使用
atof()或strtof()更改语言环境;这是setlocale(LC_ALL|~LC_NUMERIC, "");在任何呼叫atof()或类似之前打电话的问题。问题setlocale在于它对进程来说是全局的,你可能会干扰程序的其余部分。请注意,您可以使用查询当前语言环境setlocale()并在完成后恢复它。编写您自己的浮点解析例程。如果您不需要指数解析或十六进制浮点数等高级功能,这可能会非常快。
Also, note that the value 4.08cannot be represented exactly as a float; the actual value you will get is 4.0799999237060546875.
另请注意,该值4.08不能完全表示为浮点数;您将获得的实际价值是4.0799999237060546875.
回答by user1336087
Why one should not use function atof() to convert string to double?
为什么不应该使用函数 atof() 将字符串转换为双精度值?
On success, atof() function returns the converted floating point number as a double value. If no valid conversion could be performed, the function returns zero (0.0). If the converted value would be out of the range of representable values by a double, it causes undefined behavior.
成功时,atof() 函数将转换后的浮点数作为双精度值返回。如果无法执行有效转换,则该函数返回零 (0.0)。如果转换后的值超出双精度可表示值的范围,则会导致未定义行为。
Refrence:http://www.cplusplus.com/reference/cstdlib/atof/
参考:http://www.cplusplus.com/reference/cstdlib/atof/
Instead use function strtod(), it is more robust.
而是使用函数strtod(),它更健壮。
Try this code:
试试这个代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char s[100] = "4.0800";
printf("Float value : %4.8f\n",strtod(s,NULL));
return 0;
}
You will get the following output:
您将获得以下输出:
Float value : 4.08000000
Float value : 4.08000000
回答by Luchian Grigore
Use atof()
用 atof()
But this is deprecated, use this instead:
但这已被弃用,请改用它:
const char* flt = "4.0800";
float f;
sscanf(flt, "%f", &f);
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
atof()returns 0for both failure and on conversion of 0.0, best to not use it.
atof()0失败和转换都返回0.0,最好不要使用它。
回答by Kavita Jain
By using sscanf we can convert string to float.
通过使用 sscanf,我们可以将字符串转换为浮点数。
#include<stdio.h>
#include<string.h>
int main()
{
char str[100] ="4.0800" ;
const char s[2] = "-";
char *token;
double x;
/* get the first token */
token = strtok(str, s);
sscanf(token,"%f",&x);
printf( " %f",x );
return 0;
}
回答by mwp
You want to use the atof() function.
您想使用 atof() 函数。
回答by Frankoch
double x;
char *s;
s = " -2309.12E-15";
x = atof(s); /* x = -2309.12E-15 */
printf("x = %4.4f\n",x);
回答by arni rmvivek
Main() {
float rmvivek,arni,csc;
char *c="1234.00";
csc=atof(c);
csc+=55;
printf("the value is %f",csc);
}

