C中的Rounding Float并删除那些零 [英] Rounding Float in C and Remove those Zeros

查看:64
本文介绍了C中的Rounding Float并删除那些零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果在C中变量看起来像X = 10.000000,我可以将它舍入为零

小数位。即X = 10?


然后我必须将数字保存到变量中。


方法如下:

**************************************

Hi,

If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?

I then have to save the number into a variable.

The method appears below:
**************************************

printf (Round%.f \ n,10.0000000);
printf("Round % .f \n", 10.0000000);




,结果将在屏幕上显示为10


是否有任何方法可以保存舍入值为X?


谢谢,



and the result will appear on the screen as "10"

is there any method to "save" the rounded value as X?

Thanks,

推荐答案

ke ******** @ yahoo.com.hk 写道:



嗨!

如果在C中变量看起来像X = 10.000000,我可以将它四舍五入到小数位。即X = 10?


呃,是的......也许吧。但是你不会说'X`是什么:一个`int`,一个`double`?

你究竟想要实现什么?

然后我有将数字保存到变量中。


什么变量?

方法如下:
****************** ********************
Hi,
Hi!
If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?
Er, yes... maybe. But you don''t say what `X` is: an `int`, a `double`?
What exactly do you want to achieve?
I then have to save the number into a variable.
What variable?
The method appears below:
**************************************
printf(" Round%.f \ n",10.0000000);
printf("Round % .f \n", 10.0000000);



这不是将数字保存到变量中。

,结果将在屏幕上显示为10。


是(不一定在屏幕上)。

有什么方法可以保存。圆形值为X?


This is not "saving a number into a variable".
and the result will appear on the screen as "10"
Yes (not necessarily on the "screen", though).
is there any method to "save" the rounded value as X?




是的,但是您必须更具体地了解您想要什么才能获得




Yes, but you''ll have to be more specific about what exactly you want to
do.


Vladimir Oka认为:
Vladimir Oka opined:
ke ******** @ yahoo.com.hk 写道:




Hi!

如果在C中变量看起来像X = 10.000000,我可以将它四舍五入到小数位。即X = 10?
If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?



呃,是的......也许吧。但你不会说'X`是什么:一个`int`,一个
`double`?你究竟想要实现什么?



Er, yes... maybe. But you don''t say what `X` is: an `int`, a
`double`? What exactly do you want to achieve?

然后我必须将数字保存到变量中。
I then have to save the number into a variable.



什么变量?



What variable?

方法如下:
********************************* *****
The method appears below:
**************************************
> printf(" Round%.f \ n",10.0000000);
这不是将数字保存到变量中。
>printf("Round % .f \n", 10.0000000);
This is not "saving a number into a variable".
,结果将在屏幕上显示为10
and the result will appear on the screen as "10"



是(不一定在屏幕上)。< br>



Yes (not necessarily on the "screen", though).

有什么方法可以保存吗?圆形值为X?
is there any method to "save" the rounded value as X?



是的,但你必须更具体地说明你想要做什么。



Yes, but you''ll have to be more specific about what exactly you want
to do.




我也收到了Ken的回复:

您好,

感谢您回复我在Google网上论坛中的帖子。

所以我的程序如下:

*************************
Float X;(X是浮点数)
int Y;(Y是整数)。

X = 10.0000;

我能做什么我可以将10.0000变换为10
并指定Y = 10?



I have also received this reply from Ken:
Hi,

Thanks for your reply to my post in Google Groups.

So I have a prrogram as follows:

*************************
Float X;(which X is a float)
int Y;(which Y is an integer).

X = 10.0000;

what can I do so that I can transform 10.0000 into 10
and assign Y = 10?




首先,保持所有主题讨论被视为礼貌

公开。许多人甚至不发布他们的(正确的)电子邮件

地址。


回答你的问题,将10分配给`Y` ,你可以做

of:


Y = 10; / * facetious * /





X = 10.0000;

Y = X;


在后者中,只有当10实际上可以作为浮点数表示10时,'Y`才会确实为10。我认为(在调查标准之前)所有小整数都是保证的。
保证。但要注意数字,如1000000000000042.0000。


-

坚持一种观点从未被视为政治领导者的优点。

- Marcus Tullius Cicero,Ad familiares,公元前1世纪


< http://clc-wiki.net/wiki/Introduction_to_comp .lang.c>



Firstly, it is considered polite to keep all topical discussions
public. Many people don''t even publish their (correct) e-mail
addresses.

In response to your question, to assign 10 to `Y`, you can do either
of:

Y = 10; /* facetious */

or

X = 10.0000;
Y = X;

In the latter, `Y` will indeed be 10 only if 10 is exactly
representable as a floating point number on your implementation. I
think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.

--
Persistence in one opinion has never been considered
a merit in political leaders.
-- Marcus Tullius Cicero, "Ad familiares", 1st century BC

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


" Vladimir Oka"写道:
"Vladimir Oka" writes:
我>认为(在调查标准之前)保证所有小整数都是保证的。但要注意数字,如1000000000000042.0000。
I > think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.




编号例如,数字1无法准确表示。从以下系列中选择

许多术语:

1 / 2,1 / 4,1 / 8,...... 1/2 ^ googolplex


你不可能得到1分。编写I / O库

的人都知道这一点并创建*幻觉*你可以。



No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O libraries
are aware of this and create the *illusion* that you can.


这篇关于C中的Rounding Float并删除那些零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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