浮动到字符串 [英] Float to String
问题描述
如何将浮动复制到字符串?
例如:
float number = 12.34;
char numberstring [5];
*在这里介绍一些魔术代码*
导致数字字符串== ['' 1''| ''2''| ''。''| ''3''| ''4''| ''\''']
或类似的东西...我认为这将是非常简单的事情,但是在查看常见问题解答和搜索之后,这是一个很好的事情。 google我找不到合适的
方式。
谢谢。
Hi,
How could I copy a float to a string?
For example:
float number = 12.34;
char numberstring[5];
*introduce some magic code here*
resulting in numberstring == [ ''1'' | ''2'' | ''.'' | ''3'' | ''4'' | ''\0'' ]
Or soemthing similar... I thought it would be something very easy, but
after looking the FAQ and searching google I cant find an appropriate
way.
Thank you.
推荐答案
YLD于10/09/05写道:
YLD wrote on 10/09/05 :
如何将浮点数复制到字符串?
你的意思是''转换''?
浮点数= 12.34;
char numberstring [5];
*介绍这里有一些神奇的代码*
导致数字字符串== [''1''| ''2''| ''。''| ''3''| ''4''| ''\ 0'']
How could I copy a float to a string?
Do you meant ''convert'' ?
float number = 12.34;
char numberstring[5];
*introduce some magic code here*
resulting in numberstring == [ ''1'' | ''2'' | ''.'' | ''3'' | ''4'' | ''\0'' ]
sprintf()是你的朋友。确保char的目标数组是
够大。
-
Emmanuel
C-常见问题: http://www.eskimo.com/~ scs / C-faq / faq.html
C库: http://www.dinkumware.com/refxc.html
我曾经问过专家COBOL程序员,如何使用$ /
在COBOL中声明局部变量,回复是:
什么是局部变量?
sprintf() is your friend. Be sure than the destination array of char is
big enough.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
非常感谢Emmanuel,sprintf()提示就是我需要的全部内容
并且帮助很大。
然而,在搜索之后我发现asprintf()因为你没有必要处理目的地需要多少空间来满足需要,所以更加安全。这也很简单。
我发布了一个示例代码,万一有人来这里寻找
相同的信息:
#include< stdio.h>
int main()
{
char * my_string;
asprintf(& my_string,"正在%d很酷,但是免费是最好的
all。,4);
puts(my_string);
返回0;
}
再见!
Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.
However, after searching about that I found out that asprintf() is much
safer, as you don''t have to deal with how much space the destination
array needs. It is also very simple.
I post a sample code just in case anybody gets here looking for the
same information:
#include <stdio.h>
int main()
{
char *my_string;
asprintf (&my_string, "Being %d is cool, but being free is best of
all.", 4);
puts (my_string);
return 0;
}
Bye!
" YLD" < RU **** @ gmail.com>写道:
"YLD" <ru****@gmail.com> wrote:
如何将浮点数复制到字符串?
< snip>
这是一个常见问题,请参阅第13.1节(签名中的faq链接)。
简短版本:使用sprintf
我认为这将是非常容易的,[...]
Hi,
How could I copy a float to a string? <snip>
This is a faq, see section 13.1 (faq link in signature).
Short version: use sprintf
I thought it would be something very easy, [...]
它是。 :)
但是,我建议你阅读faq第12.21条关于在使用sprintf时避免
缓冲区溢出。
- -
Irrwahn Grausewitz(ir*******@freenet.de)
欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list: http://www.faqs.org/ faqs / C-faq / faq /
clc常见答案: http://benpfaff.org/writings/clc
It is. :)
However, I suggest you read section 12.21 of the faq about avoiding
buffer overruns when using sprintf.
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
这篇关于浮动到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!