C语言 c - 如何在c中简单地将浮点数转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41394063/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:33:10  来源:igfitidea点击:

How to simply convert a float to a string in c?

c

提问by Fady Sadek

FILE * fPointer;
float amount = 3.1415;
fPointer =fopen("vending.txt","w");
fprintf(fPointer ,amount);
printf("The file has been created for the first time and we added the value %f" , amount);
fclose(fPointer);

I am trying to save a float number to a text file but when i try to run this code it triggers a compiling errors because the function fprintf expects the second parameter to be an array of characters so how can i convert my float to a string so i can pass it , i come from a c# background where something like .toString() is possible so is there any thing like that in c to directly cast a float to a string ?

我正在尝试将浮点数保存到文本文件中,但是当我尝试运行此代码时,它会触发编译错误,因为函数 fprintf 期望第二个参数是一个字符数组,所以我如何将浮点数转换为字符串我可以通过它,我来自 ac# 背景,在那里可以使用 .toString() 之类的东西,所以在 c 中有任何类似的东西可以直接将浮点数转换为字符串吗?

采纳答案by René Vogt

The second parameter is the format string after which the format arguments follow:

第二个参数是格式字符串,后面是格式参数:

fprintf(fPointer, "%f", amount);

%ftells fprintfto write this argument (amount) as string representation of the float value.

%f告诉fprintf将此参数 ( amount)写为浮点值的字符串表示形式。

A list of possible format specifiers can (for example) be found here.

可以(例如)在此处找到可能的格式说明符列表。

回答by Nikola Novak

If you can use C99 standard, then the best way is to use snprintffunction. On first call you can pass it a zero-length (null) buffer and it will then return the length required to convert the floating-point value into a string. Then allocate the required memory according to what it returned and then convert safely.

如果可以使用C99标准,那么最好的方法就是使用snprintf函数。在第一次调用时,您可以向它传递一个零长度(空)缓冲区,然后它将返回将浮点值转换为字符串所需的长度。然后根据它返回的内容分配所需的内存,然后安全地转换。

This addresses the problem with sprintf that were discussed here.

这解决了此处讨论的 sprintf 问题。

Example:

例子:

int len = snprintf(null, 0, "%f", amount);
char *result = (char *)malloc(len + 1);
snprintf(result, len + 1, "%f", amount);
// do stuff with result
free(result);

回答by kumar

By using sprintf()we can convert from float to string in c language for better understanding see the below code

通过使用sprintf()我们可以在 c 语言中将浮点数转换为字符串以便更好地理解,请参见下面的代码

#include <stdio.h>
int main()
{
   float f = 1.123456789;
   char c[50]; //size of the number
    sprintf(c, "%g", f);
    printf(c);
    printf("\n");
}

Hope this will help you.

希望这会帮助你。

回答by chux - Reinstate Monica

// OP's code           v---- Format string expected here. 
// fprintf(fPointer, amount);


To print different text for each float, use the format string "%.*e".
FLT_DECIMAL_DIG - 1is the number of digits needed to print each floatvalue uniquely without undue precision.

要为每个打印不同的文本float,请使用格式字符串"%.*e"
FLT_DECIMAL_DIG - 1float唯一打印每个值而没有不适当精度所需的位数。

#include <float.h>
#include <stdio.h>

fprintf(fPointer, "%.*e", FLT_DECIMAL_DIG - 1, amount);


Using "%f"will print nearly half of small floatlike 0.000000001fand -1.2345e-10as 0.000000or -0.000000.

使用"%f"将打印将近一半的小floatlike0.000000001f-1.2345e-10as 0.000000or -0.000000

Using "%f"will print large floatlike FLT_MAXwith verbose text like "340282346638528859811704183484516925440.000000".

使用"%f"将像“340282346638528859811704183484516925440.000000”float这样FLT_MAX的冗长文本打印出大字。