printf格式的问题 [英] Problem with printf formats

查看:114
本文介绍了printf格式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:


int main(无效)

{

printf("%。3lf \\ \\ n, - 2158470 * 0.001);

}


打印


-2158.470

如果不需要,我怎么能在最后禁用''0'?


是否有可能只写%lf和只需打印多个数字

来代表数字?


I have following code:

int main(void)
{
printf("%.3lf\n",-2158470*0.001);
}

it prints

-2158.470

How can i disable ''0'' at the end, if they are not needed ?

Is there a possibility just to write %lf and just print as many digits
as needed to represent the number ?


推荐答案

<猜测者说:

(...)printf("%。3lf \ n", - 2158470 * 0.001);
(...)打印

-2158.470

如果不需要,我怎么能在最后禁用''0'?是否有可能只写%lf并打印所需的数字来代表数字?
(...) printf("%.3lf\n",-2158470*0.001); (...) it prints

-2158.470

How can i disable ''0'' at the end, if they are not needed ? Is there a
possibility just to write %lf and just print as many digits as needed to
represent the number ?




问题不在在printf()函数中,但以浮点方式

数字由您的计算机表示。由于舍入错误,浮点数b $ b点数几乎没有你想象的''确切'值。搜索

谷歌搜索浮点以获取一些细节,或阅读此维基百科页面:
http://en.wikipedia.org/wiki/Floating_point


您可以自行选择

数字中有多少位数是重要的,所以你应该告诉printf()这个。

-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



The problem is not in the printf() function, but in the way floating point
numbers are represented by your computer. Due to rounding errors, floating
point numbers hardly ever have the ''exact'' value you would expect. Search
google for ''floating point'' for some details, or read this wikipedia page:
http://en.wikipedia.org/wiki/Floating_point.

It is up to you to make the choice how many digits are significant in a
number, so that is why you should tell printf() about this.
--
:wq
^X^Cy^K^X^C^C^C^C


Guenther Sohler写道:
Guenther Sohler wrote:

我有以下代码:

int main(无效)
{/> printf("%。3lf \ n", - 2158470 * 0.001);
}

它打印

-2158.470

如果不是,我怎么能在最后禁用''0'需要吗?

是否有可能只写%lf并打印多少
数字来表示数字?

I have following code:

int main(void)
{
printf("%.3lf\n",-2158470*0.001);
}

it prints

-2158.470

How can i disable ''0'' at the end, if they are not needed ?

Is there a possibility just to write %lf and just print as many
digits as needed to represent the number ?




你问的是错误的问题。

代表数字需要尾随零。否则你怎么能区别于:


printf("%。2f \ n",-2158471 * 0.001)


(总是假设-2158471没有造成溢出)


我相信你的问题是如何抑制拖尾零。一个

的小想法和一个中间缓冲区应该给你一个

的合适答案。


-

答:因为它会破坏人们通常阅读文本的顺序。

问:为什么顶级发布这么糟糕的事情?

A:Top-发布。

问:usenet和电子邮件中最烦人的事情是什么?



You are asking the wrong question. The trailing zero is needed to
represent the number. Otherwise how could you distinguish it from:

printf("%.2f\n", -2158471 * 0.001)

(always assuming that -2158471 does not create an overflow)

Your question, I believe, is "how to suppress trailing zeroes". A
little thought and an intermediary buffer should give you a
suitable answer for that.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Guenther Sohler:
Guenther Sohler:
int main(void)
{
printf("%。3lf \ n", - 2158470 * 0.001);
}
它打印

-2158.470

如果不需要,我怎么能在最后禁用''0'?

有没有?有可能只写%lf并打印多个数字
根据需要来表示数字?
int main(void)
{
printf("%.3lf\n",-2158470*0.001);
}

it prints

-2158.470

How can i disable ''0'' at the end, if they are not needed ?

Is there a possibility just to write %lf and just print as many digits
as needed to represent the number ?




l in%lf是多余的,只需使用%f。

为避免尾随零,请尝试%g。


Jirka



The l in %lf is superfluous, just use %f.
To avoid the trailing zeros try %g.

Jirka


这篇关于printf格式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆