如何将字符串转换为浮点数?

发布于 2024-12-13 06:20:41 字数 288 浏览 2 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

各自安好 2024-12-20 06:20:42

使用 atof()strtof()* 代替:

printf("float value : %4.8f\n" ,atof(s)); 
printf("float value : %4.8f\n" ,strtof(s, NULL)); 

https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/

  • atoll( ) 用于整数。
  • atof()/strtof() 用于浮点数。

使用 atoll() 只能得到 4.00 的原因是它在找到第一个非数字时停止解析。

*请注意,strtof() 需要 C99 或 C++11。

Use atof() or strtof()* instead:

printf("float value : %4.8f\n" ,atof(s)); 
printf("float value : %4.8f\n" ,strtof(s, NULL)); 

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 with atoll() is because it stops parsing when it finds the first non-digit.

*Note that strtof() requires C99 or C++11.

べ映画 2024-12-20 06:20:42

不幸的是,没有办法轻松做到这一点。每个解决方案都有其缺点。

  1. 直接使用atof()strtof():这是大多数人会告诉你的做法,而且大多数时候它都会起作用。但是,如果程序设置区域设置或使用设置区域设置的库(例如,显示本地化菜单的图形库),并且用户将其区域设置设置为小数点分隔符不是 .< 的语言。 /code> (例如 fr_FR ,其中分隔符是 ,)这些函数将在 . 处停止解析,您仍然会得到 4.0.

  2. 使用atof()strtof()但更改语言环境;这是在调用 atof() 或类似函数之前调用 setlocale(LC_ALL|~LC_NUMERIC, ""); 的问题。 setlocale 的问题是它对于进程来说是全局的,并且您可能会干扰程序的其余部分。请注意,您可以使用 setlocale() 查询当前区域设置,并在完成后恢复它。

  3. 编写您自己的浮点解析例程。如果您不需要指数解析或十六进制浮点数等高级功能,这可能会很快。

另请注意,值 4.08 不能精确地表示为浮点数;您将获得的实际值是4.0799999237060546875

Unfortunately, there is no way to do this easily. Every solution has its drawbacks.

  1. Use atof() or strtof() 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 as fr_FR where the separator is ,) these functions will stop parsing at the . and you will stil get 4.0.

  2. Use atof() or strtof() but change the locale; it's a matter of calling setlocale(LC_ALL|~LC_NUMERIC, ""); before any call to atof() or the likes. The problem with setlocale 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 with setlocale() and restore it after you're done.

  3. 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 is 4.0799999237060546875.

∞觅青森が 2024-12-20 06:20:42

为什么不应该使用函数 atof() 将字符串转换为双精度?

成功后,atof() 函数将转换后的浮点数作为双精度值返回。
如果无法执行有效转换,该函数将返回零 (0.0)。
如果转换后的值超出双精度值的可表示值范围,则会导致未定义的行为

参考:http://www.cplusplus.com/reference/cstdlib/atof/

而是使用函数strtod(),它更加健壮。

试试这段代码:

#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;
}

您将得到以下输出:

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:

#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

滥情哥ㄟ 2024-12-20 06:20:42

使用 atof()

但这已被弃用,请使用它:

const char* flt = "4.0800";
float f;
sscanf(flt, "%f", &f);

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

atof() 对于失败和转换都返回 0 0.0,最好不要使用它。

Use 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/

atof() returns 0 for both failure and on conversion of 0.0, best to not use it.

落花随流水 2024-12-20 06:20:42

通过使用 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 using sscanf we can convert string to float.

#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; 
}
一页 2024-12-20 06:20:42

提醒:
使用 atof() 时,请确保字符串中没有“”。
atof("1.123") 将返回 0.000 或类似的值。

解决方案

str_val[0] = "0";
str_val[len-1] = "\n"; //len = length of string

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

str_val[0] = "0";
str_val[len-1] = "\n"; //len = length of string
抠脚大汉 2024-12-20 06:20:42
double x;

char *s;

s = " -2309.12E-15";

x = atof(s);     /* x = -2309.12E-15 */

printf("x = %4.4f\n",x);
double x;

char *s;

s = " -2309.12E-15";

x = atof(s);     /* x = -2309.12E-15 */

printf("x = %4.4f\n",x);
所谓喜欢 2024-12-20 06:20:42
Main()  {
    float rmvivek,arni,csc;
    char *c="1234.00";
    csc=atof(c);
    csc+=55;
    printf("the value is %f",csc);
}
Main()  {
    float rmvivek,arni,csc;
    char *c="1234.00";
    csc=atof(c);
    csc+=55;
    printf("the value is %f",csc);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文