vb.net 将“Double”或“Single”转换为“Integer”的最简洁方法,无需四舍五入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14691978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cleanest way to convert a `Double` or `Single` to `Integer`, without rounding
提问by supercat
Converting a floating-point number to an integer using either CIntor CTypewill cause the value of that number to be rounded. The Intfunction and Math.Floormay be used to convert a floating-point number to a whole number, rounding toward negative infinity, but both functions return floating-point values which cannot be implicitly used as Integervalues without a cast.
使用CInt或CType将浮点数转换为整数将导致该数字的值四舍五入。的Int功能和Math.Floor可以用于一个浮点数转换为整数,向负无穷舍入,但是两个函数都返回其不能隐用作浮点值Integer的值不进行强制转换。
Is there a concise and idiomatic alternative to IntVar = CInt(Int(FloatingPointVar));? Pascal included Roundand Truncfunctions which returned Integer; is there some equivalent in either the VB.NET language or in the .NET framework?
是否有简洁而惯用的替代方法IntVar = CInt(Int(FloatingPointVar));?包括 PascalRound和Trunc返回的函数Integer;在 VB.NET 语言或 .NET 框架中是否有一些等价物?
A similar question, CInt does not round Double value consistently - how can I remove the fractional part?was asked in 2011, but it simply asked if there was a way to convert a floating-point number to an integer; the answers suggested a two-step process, but it didn't go into any depth about what does or does not exist in the framework. I would find it hard to believe that the Framework wouldn't have something analogous to the PascalTruncfunction, given that such a thing will frequently be needed when performing graphical operations using floating-point operands [such operations need to be rendered as discrete pixels, and should be rounded in such a way that round(x)-1 = round(x-1) for all x that fit within the range of +/- (2^31-1); even if such operations are rounded, they should use Floor(x+0.5), rather than round-to-nearest-even, so as to ensure the above property]
一个类似的问题,CInt 不会始终如一地舍入 Double 值 - 如何删除小数部分?在 2011 年被问到,但它只是询问是否有办法将浮点数转换为整数;答案提出了一个两步过程,但它没有深入探讨框架中存在或不存在的内容。我很难相信框架没有类似于Pascal 的东西Trunc函数,考虑到在使用浮点操作数执行图形操作时经常需要这样的事情[此类操作需要呈现为离散像素,并且应该以round(x)-1 = round(x -1) 对于所有在 +/- (2^31-1) 范围内的 x;即使这样的操作是四舍五入的,也应该使用Floor(x+0.5),而不是四舍五入,以保证上述性质]
Incidentally, in C# a typecast from Doubleto Intusing (type)exprnotation uses round-to-zero semantics; the fact that this differs from the VB.NET behavior suggests that one or both languages is using its own conversion routines rather an explicit conversion operator included in the Framework. It would seem likely that the Framework should define a conversion operator? Does such an operator exist within the framework? What does it do? Is there a way to invoke it from C# and/or VB.NET?
顺便说一句,在 C# 中Double,Int使用(type)expr符号从to的类型转换使用舍入到零语义;这与 VB.NET 行为不同的事实表明,一种或两种语言正在使用自己的转换例程,而不是框架中包含的显式转换运算符。框架似乎应该定义一个转换运算符?框架中是否存在这样的运算符?它有什么作用?有没有办法从 C# 和/或 VB.NET 调用它?
采纳答案by Konrad Rudolph
After some searching, it seems that VB has no clean way of accomplishing that, short of writing an extension method.
经过一番搜索,似乎VB没有干净的方法来实现这一点,缺少编写扩展方法。
The C# (int)cast translates directly into conv.i4in IL. VB has no such operators, and no framework function seems to provide an alternative.
C# 类型(int)转换直接转换conv.i4为 IL。VB 没有这样的操作符,而且似乎没有框架函数提供替代方案。
Usenet had an interesting discussion about this back in 2005–?of course a lot has changed since then but I think this still holds.
Usenet 早在 2005年就对此进行了有趣的讨论——当然从那时起发生了很多变化,但我认为这仍然成立。
回答by Styxxy
You can use the Math.Truncatemethod.
您可以使用该Math.Truncate方法。
Calculates the integral part of a specified double-precision floating-point number.
计算指定双精度浮点数的整数部分。
For example:
例如:
Dim a As double = 1.6666666
Dim b As Integer = Math.Truncate(a) ' b = 1
回答by Ed Ajaz
I know this is an old case but I saw no one suggest the Math.Round() function.
我知道这是一个旧案例,但我看到没有人建议使用 Math.Round() 函数。
Yes Math.Round takes a double and returns a double. However it returns a number that has been rounded to a whole number. It should easily and concisely convert to an integer using cInt. Would that suffice?
是的 Math.Round 需要一个双精度值并返回一个双精度值。但是,它返回一个四舍五入为整数的数字。它应该使用 cInt 轻松简洁地转换为整数。这样就够了吗?
cInt(math.round(10000.54564)) ' = 10001 cInt(math.round(10000.49564)) ' = 10000
cInt(math.round(10000.54564)) ' = 10001 cInt(math.round(10000.49564)) ' = 10000
回答by lindosekai
You may need extract the Int part of a float number:
您可能需要提取浮点数的 Int 部分:
float num = 12.234;
string toint = "" + num;
string auxil = toint.Split('.');
int newnum = Int.Parse(auxil[0]);

