回合 [英] Round

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

问题描述

您好,


我有以下内容:

int a; int b; var c


c = a /(float)b;


我用float来获取int值作为结果。我认为在C#中我必须。


但是我知道我想将c舍入为3.48或7.50,...


我是使用Math.Round(c,2)但我得到错误:

不能隐式地将类型''double''转换为''float''。一个明确的

转换存在(你错过了一个演员吗?)


我也试过Math.Round((double)c,2)


我做错了什么?


谢谢,

Miguel

Hello,

I have the following:
int a; int b; var c

c = a / (float)b;

I used float to not get an int value as result. I think in C# I must.

But know I want to round c to something like 3.48 or 7.50, ...

I am using Math.Round(c,2) but I get the error:
Cannot implicitly convert type ''double'' to ''float''. An explicit
conversion exists (are you missing a cast?)

I also tried Math.Round((double)c,2)

What am I doing wrong?

Thanks,
Miguel

推荐答案

2008年11月18日星期二15:13:16 -0800,shapper< md ***** @ gmail.comwrote:
On Tue, 18 Nov 2008 15:13:16 -0800, shapper <md*****@gmail.comwrote:

你好,


我有以下内容:

int a; int b; var c


c = a /(float)b;


我用float来获取int值作为结果。我认为在C#中我必须。


但是我知道我想将c舍入为3.48或7.50,...


我是使用Math.Round(c,2)但我得到错误:

不能隐式地将类型''double''转换为''float''。一个明确的

转换存在(你错过了一个演员吗?)


我也试过Math.Round((double)c,2)


我做错了什么?
Hello,

I have the following:
int a; int b; var c

c = a / (float)b;

I used float to not get an int value as result. I think in C# I must.

But know I want to round c to something like 3.48 or 7.50, ...

I am using Math.Round(c,2) but I get the error:
Cannot implicitly convert type ''double'' to ''float''. An explicit
conversion exists (are you missing a cast?)

I also tried Math.Round((double)c,2)

What am I doing wrong?



通过指定类型为float的表达式c的类型为c,c的类型为c。风...

up isfloat。大概是你试图将结果分配给c和c。但是你不能隐含地将类型''double''

(这是Math.Round()返回的)转换为''float''。但是,你可以使用

明确的演员表。


你忘了把它包括在内吗?


Pete

By assigning an expression of type "float" to "c", the type of "c" winds
up being "float. Presumably you are trying to assign the result of
Math.Round() to "c" but you can''t implicitly convert the type ''double''
(which is what Math.Round() is returning) to ''float''. But, there is an
explicit cast that you can use.

Did you forget to include it?

Pete


11月18日,11:41 * pm,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Nov 18, 11:41*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

2008年11月18日星期二15:13:16 -0800 ,shapper< mdmo ... @ gmail.comwrote:
On Tue, 18 Nov 2008 15:13:16 -0800, shapper <mdmo...@gmail.comwrote:

Hello,
Hello,


我有以下内容:

int a; int b; var c
I have the following:
int a; int b; var c


c = a /(float)b;
c = a / (float)b;


我使用float来获取int值作为结果。我认为在C#中我必须这样做。
I used float to not get an int value as result. I think in C# I must.


但我知道我想将c舍入为3.48或7.50,...
But know I want to round c to something like 3.48 or 7.50, ...


我正在使用Math.Round(c,2)但是我收到错误:

无法隐式地将类型''double''转换为''float''。存在明确的

转换(你是否错过了演员?)
I am using Math.Round(c,2) but I get the error:
Cannot implicitly convert type ''double'' to ''float''. An explicit
conversion exists (are you missing a cast?)


我也试过Math.Round((double) c,2)
I also tried Math.Round((double)c,2)


我做错了什么?
What am I doing wrong?



通过指定类型为float的表达式c的类型为c,c的类型为c。风*

up is浮动。 *据推测,您试图将*

Math.Round()的结果分配给c。但你不能隐含地将类型''double''*

(这是Math.Round()返回的)转换为''float''。 *但是,你可以使用*

明确的演员表。


你忘了把它包括在内吗?


Pete


By assigning an expression of type "float" to "c", the type of "c" winds *
up being "float. *Presumably you are trying to assign the result of *
Math.Round() to "c" but you can''t implicitly convert the type ''double'' *
(which is what Math.Round() is returning) to ''float''. *But, there is an*
explicit cast that you can use.

Did you forget to include it?

Pete



我发布了一个更简单的例子来试图解决这个问题...但我的完整

代码是:

TagsPerFile =(float)Math.Round((double)(database.FilesTags.Coun t()/

(float)(database.Files.Count()== 0 ?1:database.Files.Count())),2)


TagsPerFile是一个浮点数......但是当我显示值时它不是

圆润...


我使用浮动因为我不需要双精度的所有精度...

但是我应该使用双作为一般类型?


谢谢,

Miguel

I posted a simpler example to try to figure this out ... but my full
code is:
TagsPerFile = (float)Math.Round((double)(database.FilesTags.Coun t() /
(float)(database.Files.Count() == 0 ? 1 : database.Files.Count())), 2)

TagsPerFile is a float ... however when I display the value it is not
rounded ...

I used float because I don''t need all the precision of the double ...
but should I use a double as a general type?

Thanks,
Miguel


在星期二, 2008年11月18日15:51:35 -0800,shapper< md ***** @ gmail.comwrote:
On Tue, 18 Nov 2008 15:51:35 -0800, shapper <md*****@gmail.comwrote:

我发布了一个更简单的例子来试图计算这个...但我的完整

代码是:

TagsPerFile =(flo at)Math.Round((double)(database.FilesTags.Coun t()/

(float)(database.Files.Count()== 0? 1:database.Files.Count())),2)
I posted a simpler example to try to figure this out ... but my full
code is:
TagsPerFile = (float)Math.Round((double)(database.FilesTags.Coun t() /
(float)(database.Files.Count() == 0 ? 1 : database.Files.Count())), 2)



所有应有的尊重,这几乎不是完整代码。它肯定比你之前发布的更复杂,但它只是一个声明,并且

甚至没有带有所需分号的声明。

All due respect, that''s hardly "full code". It''s certainly more
complicated than what you posted before, but it''s just one statement, and
not even one with the required semicolon.


TagsPerFile是一个浮点数...但是当我显示它的值时它不是

舍入...
TagsPerFile is a float ... however when I display the value it is not
rounded ...



如果你无法编译代码,你如何显示价值?


为什么你说这不是四舍五入?什么是原始值,什么是

您之前声明的声明返回的值甚至不会是b
编译?


请记住,无论你使用float还是double,对于浮点变量可以表示的数字都有限制。无论您使用何种浮点格式,舍入值都可以到目前为止。

How do you display the value if you can''t get the code to compile?

Why do you say it''s not rounded? What was the original value, and what is
the value returned by the statement that you earlier stated wouldn''t even
compile?

Keep in mind that whether you use float or double, there are limits to
what numbers a floating point variable can represent. Rounding values can
only go so far, regardless of the floating point format you''re using.


我使用浮点数因为我不喜欢不需要双倍的所有精度...

但是我应该使用双倍作为一般类型吗?
I used float because I don''t need all the precision of the double ...
but should I use a double as a general type?



两者都没问题。除非这是性能或内存瓶颈,否则它不会产生任何影响。但是,如果你真的需要将_numeric_

值精确地舍入到两位小数,那么你应该使用十进制来代替
,而不是使用float _or_ double。


请注意,在大多数情况下,一个_doesn''t_需要数值为

舍入_exactly_到(例如)两位小数。只需使用数据类型可能最接近的表示就足够了,并且如果需要向用户显示值或
数字格式/>
否则将其表示为文本。


Pete

Either is fine. Unless this is a performance or memory bottleneck, it''s
unlikely to make a difference. But, if you really need the _numeric_
value to be rounded exactly to two decimal places, you should probably be
using decimal, rather than float _or_ double.

Note that in most cases, one _doesn''t_ need the numeric value to be
rounded _exactly_ to (for example) two decimal places. It''s sufficient to
just use the closest representation possible for the data type, and use
numeric formatting if you need to display the value to the user or
otherwise represent it as text.

Pete


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

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