如何将字符串转换为浮点数?
#include<stdio.h>
#include<string.h>
int main()
{
char s[100] ="4.0800" ;
printf("float value : %4.8f\n" ,(float) atoll(s));
return 0;
}
我预计输出应该是 4.08000000
而我只得到 4.00000000
。
有什么办法可以得到点后的数字吗?
#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.08000000
whereas I got only 4.00000000
.
Is there any way to get the numbers after the dot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用
atof()
或strtof()
* 代替:https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/
atoll( )
用于整数。atof()
/strtof()
用于浮点数。使用
atoll()
只能得到4.00
的原因是它在找到第一个非数字时停止解析。*请注意,
strtof()
需要 C99 或 C++11。Use
atof()
orstrtof()
* instead:https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/
atoll()
is meant for integers.atof()
/strtof()
is for floats.The reason why you only get
4.00
withatoll()
is because it stops parsing when it finds the first non-digit.*Note that
strtof()
requires C99 or C++11.不幸的是,没有办法轻松做到这一点。每个解决方案都有其缺点。
直接使用
atof()
或strtof()
:这是大多数人会告诉你的做法,而且大多数时候它都会起作用。但是,如果程序设置区域设置或使用设置区域设置的库(例如,显示本地化菜单的图形库),并且用户将其区域设置设置为小数点分隔符不是.< 的语言。 /code> (例如
fr_FR
,其中分隔符是,
)这些函数将在.
处停止解析,您仍然会得到4.0
.使用
atof()
或strtof()
但更改语言环境;这是在调用atof()
或类似函数之前调用setlocale(LC_ALL|~LC_NUMERIC, "");
的问题。setlocale
的问题是它对于进程来说是全局的,并且您可能会干扰程序的其余部分。请注意,您可以使用setlocale()
查询当前区域设置,并在完成后恢复它。编写您自己的浮点解析例程。如果您不需要指数解析或十六进制浮点数等高级功能,这可能会很快。
另请注意,值
4.08
不能精确地表示为浮点数;您将获得的实际值是4.0799999237060546875
。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_FR
where 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 withsetlocale
is 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.
Also, note that the value
4.08
cannot be represented exactly as a float; the actual value you will get is4.0799999237060546875
.为什么不应该使用函数 atof() 将字符串转换为双精度?
成功后,atof() 函数将转换后的浮点数作为双精度值返回。
如果无法执行有效转换,该函数将返回零 (0.0)。
如果转换后的值超出双精度值的可表示值范围,则会导致未定义的行为。
参考:http://www.cplusplus.com/reference/cstdlib/atof/
而是使用函数strtod(),它更加健壮。
试试这段代码:
您将得到以下输出:
Float value : 4.08000000
Why one should not use function atof() to convert string to double?
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.
Refrence:http://www.cplusplus.com/reference/cstdlib/atof/
Instead use function strtod(), it is more robust.
Try this code:
You will get the following output:
Float value : 4.08000000
使用
atof()
但这已被弃用,请使用它:
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
atof()
对于失败和转换都返回0
0.0
,最好不要使用它。Use
atof()
But this is deprecated, use this instead:
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
atof()
returns0
for both failure and on conversion of0.0
, best to not use it.通过使用 sscanf 我们可以将字符串转换为浮点数。
By using sscanf we can convert string to float.
提醒:
使用 atof() 时,请确保字符串中没有“”。
atof("1.123") 将返回 0.000 或类似的值。
解决方案
Reminder:
While using atof(), make sure you don't have "" in your string.
atof("1.123") will return 0.000 or something like that.
Solution
您想使用 atof() 函数。
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
You want to use the atof() function.
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/