在VB.NET中不能真正实现Double to Integer转换?!? [英] Can't to real Double to Integer cast in VB.NET?!?

查看:494
本文介绍了在VB.NET中不能真正实现Double to Integer转换?!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们一直在做一些小实验,看起来VB.NET并没有
直接相当于C#double到整数强制转换。


Dim d为Double = 2.5#

Dim i as Integer = CType(d,Integer)


被反编译为


double d = 2.5;

int i =(int)Math.Round(d);





Dim d为Double = 2.5#

Dim i as Integer = CType(Math.Floor(d),Integer)


被反编译为


double d = 2.5;

int i =(int)Math.Round(Math.Floor (d));

那么你怎么能在VB.NET中直接演员? DirectCast

不适用于值类型,CType函数总是添加一个

Math.Round调用。 CInt(d)给出与CType(d,Integer)相同的结果。


Convert.ToInt32()函数也进行舍入。


谢谢,


Sam


We''ve been doing a little experimenting and it seems VB.NET doesn''t
have a direct equivalent to a C# double to integer cast.

Dim d as Double = 2.5#
Dim i as Integer = CType(d, Integer)

Is decompiled to

double d = 2.5;
int i = (int)Math.Round(d);

and

Dim d as Double = 2.5#
Dim i as Integer = CType(Math.Floor(d),Integer)

Is decompiled to

double d = 2.5;
int i = (int)Math.Round(Math.Floor(d));
So how the heck can you do a straight cast in VB.NET? DirectCast
doesn''t work on value types and the CType functions always add a
Math.Round call. CInt(d) gives same results as CType(d, Integer).

The Convert.ToInt32() function also does rounding.

Thanks,

Sam

推荐答案

如果你的整数得到2那么就没什么了错误,因为整数不是

应该有小数。


chanmm


" Samuel R. Neff" < BL **** @ newsgroup.nospam>在留言中写道

新闻:a3 ******************************** @ 4ax.com ...
If you get 2 in your integer then is nothing wrong because integer is not
supposed to have decimal.

chanmm

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:a3********************************@4ax.com...

我们一直在做一些小实验,似乎VB.NET没有直接等同于C#double到整数强制转换。

Dim d为Double = 2.5#
Dim i as Integer = CType(d,Integer)

被反编译为

d = 2.5;
int i =(int)Math.Round(d);



Dim d as Double = 2.5#
Dim我作为整数= CType(Math.Floor(d),整数)

被反编译为

双d = 2.5;
int i =(int)Math .Round(Math.Floor(d));

那么你怎么能在VB.NET中直接演员? DirectCast
不适用于值类型,CType函数总是添加一个Math.Round调用。 CInt(d)给出与CType(d,Integer)相同的结果。

Convert.ToInt32()函数也进行舍入。

谢谢,

Sam

We''ve been doing a little experimenting and it seems VB.NET doesn''t
have a direct equivalent to a C# double to integer cast.

Dim d as Double = 2.5#
Dim i as Integer = CType(d, Integer)

Is decompiled to

double d = 2.5;
int i = (int)Math.Round(d);

and

Dim d as Double = 2.5#
Dim i as Integer = CType(Math.Floor(d),Integer)

Is decompiled to

double d = 2.5;
int i = (int)Math.Round(Math.Floor(d));
So how the heck can you do a straight cast in VB.NET? DirectCast
doesn''t work on value types and the CType functions always add a
Math.Round call. CInt(d) gives same results as CType(d, Integer).

The Convert.ToInt32() function also does rounding.

Thanks,

Sam






我认为这是VB.NET的功能。


转换主要是纯IL语句。这里最好的例子是

从Double转换为Integer。这可以编译为

Math.Round,然后是conv.i4.ovf指令。额外的

电话的目的是实现银行家的四舍五入,CLR本身并不支持
。 (在银行家的四舍五入中,两个

整数之间的等距十进制数四舍五入到最接近的偶数。所以3.5轮到4,

但是2.5轮到2.)在这种情况下,调用Convert.ToInt32(Double)不是和CInt(Double)相同的
,你必须选择你想要的语义。


VB中的转换运算符
http://www.panopticoncentral.net/arc...6/07/1200.aspx


祝你好运,


Perter Huang

微软在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有授予任何权利。

Hi

I think this is VB.NET''s feature.

Conversions that are mostly pure IL statements. The best example here is
the conversion from Double to Integer. This compiles down to a call to
Math.Round and then a conv.i4.ovf instruction. The purpose of the extra
call is to implement banker''s rounding, which the CLR doesn''t natively
support. (In banker''s rounding, a decimal number equidistant between two
whole numbers is rounded to the nearest even number. So 3.5 rounds to 4,
but 2.5 rounds to 2.) In this case, calling Convert.ToInt32(Double) is not
the same as CInt(Double), and you have to choose which semantic you desire.

Conversion operators in VB
http://www.panopticoncentral.net/arc...6/07/1200.aspx

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.




但是没有添加Math.Round操作删除我们的选项关于

如何处理转换并添加额外不必要的函数调用和

处理?当然,对于使用这两种语言工作的人来说,C#中的演员表演的内容与

演员阵营不同,这是非常令人困惑的。在VB.NET中。


此外,额外的循环操作增加了额外的功能

和处理时间,这可能是完全没必要的。以此为例:

此代码:


C#:


int i =(int)2.5#;


VB.NET


Dim i as Integer = CType(Math.Floor(2.5),Integer)


在C#的情况下,它是一个强制转换,所以我们知道十进制将被截断

我将在VB.NET中拥有2.我们必须知道CType很奇怪/>
通过添加圆形函数来实现非标准的投射行为,因此

必须在转换之前自行调用Floor。

此外,因为我们调用了Math .Floor()调用Math.Round()

由编译器添加完全没用 - 结果来自

Math.Floor已经是不可或缺的了,它'只是存储为浮动点

值。


Sam


2005年1月26日星期三格林威治标准时间01:45:34, v-******@online.microsoft.com />
(Peter Huang[MSFT])写道:

But doesn''t adding that Math.Round operation remove our options on how
to handle conversions and add extra unnecessary function calls and
processing? Certainly for someone working in both languages, it''s
very confusing that a cast in C# does something different than a
"cast" in VB.NET.

Furthermore, the additional round operation adds extra functionality
and processing time that may be totally unnecessary. Take for example
this code:

C#:

int i = (int)2.5#;

VB.NET

Dim i as Integer = CType(Math.Floor(2.5),Integer)

In the C# case it''s a cast so we know the decimal will be truncated
and i will have 2. in VB.NET, we have to know that CType does weird
non-standard casting behavior by adding a round function and therefore
have to call Floor ourselves before doing the conversion.
Furthermore, since we called Math.Floor() the call to Math.Round()
which is added by the compiler is totally useless--the result from
Math.Floor is already integral, it''s just stored as a floating point
value.

Sam

On Wed, 26 Jan 2005 01:45:34 GMT, v-******@online.microsoft.com
("Peter Huang" [MSFT]) wrote:


我认为这是VB.NET的功能。

转换主要是纯IL语句。这里最好的例子是从Double到Integer的转换。这将编译为对Math.Round的调用,然后是conv.i4.ovf指令。额外
电话的目的是实现银行家的四舍五入,而CLR本身并不支持。 (在银行家的四舍五入中,两个
整数之间等距的十进制数四舍五入到最接近的偶数。所以3.5轮到4,
但是2.5轮到2.)在这种情况下,调用Convert.ToInt32(Double)与CInt(Double)不一样,你必须选择你想要的语义。

VB中的转换运算符 http://www.panopticoncentral.net/arc...6/ 07 / 1200.aspx

致以最诚挚的问候,

Perter Huang
微软在线合作伙伴支持

获得安全! - www.microsoft.com/security
此帖子提供按原样没有保证,也没有权利。
Hi

I think this is VB.NET''s feature.

Conversions that are mostly pure IL statements. The best example here is
the conversion from Double to Integer. This compiles down to a call to
Math.Round and then a conv.i4.ovf instruction. The purpose of the extra
call is to implement banker''s rounding, which the CLR doesn''t natively
support. (In banker''s rounding, a decimal number equidistant between two
whole numbers is rounded to the nearest even number. So 3.5 rounds to 4,
but 2.5 rounds to 2.) In this case, calling Convert.ToInt32(Double) is not
the same as CInt(Double), and you have to choose which semantic you desire.

Conversion operators in VB
http://www.panopticoncentral.net/arc...6/07/1200.aspx

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.






这篇关于在VB.NET中不能真正实现Double to Integer转换?!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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