如何将7.3456788e-12值转换为字符串格式以进行显示? [英] How to convert 7.3456788e-12 value to string format for displaying purpose?
问题描述
大家好,
我只是想将大的浮点值转换为字符串格式.
在我的程序中,
Hi guys,
I just got stuck with conversion of a big floating value to string format.
In my program,
void double_to_string(long double v)
{
unsigned char res[40];
// here i am using compiler library function sprintf() which will give us the string format
sprintf(res,"%f",v);
if(strlen(res)>14)
sprintf(res,"%Le",v);// if the result is having more number of digits
return(res);
}
void fun1()
{
long double v;
unsigned char *result_str;
v = 0.56726*6.456*1e-12; // based on some calculations i used to get this value
result_str = double_to_string(v);
print_result(result_str);
}
当我调试代码时,在变量值中,我在double_to_string
函数中找到了v
的值,例如 3.66223056e-12 .但是在我的sprintf()
函数中给出0.000000作为结果.无法将这种类型的值转换为字符串.我如何克服这个问题以获得正确的结果.
注意:我的主要目的是进行如此大的计算,并将其转换为更准确的字符串格式. (就像我们的PC计算器一样)
由于显示区域的限制,如果结果字符串具有更多数字,我将选择指数结果.
我希望我能清楚地提到我的问题.所以,如果有人可以解决这个问题,请帮帮我.对不起我的英语不好.
When i debug my code, in variable values i found the value of v
in double_to_string
function like this 3.66223056e-12. But in my sprintf()
function giving 0.000000 as result. It''s unable to convert such type of value to string. How can i over come this to get the correct results.
Note: My main intention is doing such big calculations and converting them in strings format with more accuracy. (Like our PC calculator)
Due to limitations of my display area, i am choosing exponent results if the resultant string is having more digits.
I hope i mentioned my problem clearly.So, If can any one have solution for this please help me out.Excuse for my poor english.
推荐答案
您对sprintf()
向后,输出字符串先行,然后是format语句,然后是要格式化为字符串的值.请参见此处 [
Your arguments tosprintf()
are backwards, the output string goes first, then the format statement, then the values to be formatted into the string. See here[^]
Also, the default precision (6) is not usually adequate for very small numbers. anything e-12 is pretty small. Try something like %.20f :)
查看格式类型说明符 [ ^ ]一款适合您的需求.对于您的情况,我建议使用g
代码作为最佳选择.
Look at the format type specifiers[^] for the one that suits your needs. In your case I would suggest the g
code as the best choice.
这篇关于如何将7.3456788e-12值转换为字符串格式以进行显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!