问题描述
我偶然发现了一个问题, CINT 并转换双整数。
I've stumbled upon an issue with CInt and converting a double to an integer.
这个问题是这样的:
CInt(10.5) 'Result is 10
CInt(10.51) 'Result it 11, but I expected 10...
我习惯了C#风格转换,其中(INT)10.51
10
正如在有关 Integer.Parse VS CINT 的问题,结果却四舍五入以某种方式。
As pointed out in the question about Integer.Parse vs CInt, the result is just rounded in some fashion.
不过,我需要的是得到的只有整数部分,扔掉小数之一。我怎样才能达到这样的类型转换在VB.NET呢?经过一番研究,我发现我可以使用修正()
函数来达到目的,但它是最好的选择吗?
However, all I need is to get only integer part and throw away the fractional one. How can I achieve such type of conversion in VB.NET? After some research I see that I can use the Fix()
function to do the trick, but is it the best choice?
推荐答案
您可以使用诠释
或修正
功能但这些函数的返回值类型是双所以你必须把它转换为整数,如果选项严格
是在
。
You may use Int
or Fix
functions but return value type of these functions is double so you have to convert it to Integer if option strict
is on
.
no = Convert.ToInt32(Int(10.51))
这篇关于CINT不圆Double值一致 - 我怎么能去掉小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!