之間有什麼區別:CINT與Math.Round在Visual Basic .NET
Dim a As Integer = CInt(2.2)
和
Dim a As Integer = Math.Round(2.2)
?
之間有什麼區別:CINT與Math.Round在Visual Basic .NET
Dim a As Integer = CInt(2.2)
和
Dim a As Integer = Math.Round(2.2)
?
這兩個功能之間的差別是它們做完全不同的事情:
CInt
轉換爲Integer
類型Math.Round
輪值到最接近Integer
Math.Round
在這種情況下會得到2.0
,如MSDN documentation所指定。您也在錯誤地使用該功能,請參閱上面的MSDN鏈接。如果轉換失敗,您可以使用Try..Catch
for this。
備註:你是VB.NET的新手,但你可能想嘗試切換到C#。我發現它是一個VB.NET & C++的混合體,它比VB.NET更容易處理。
CInt返回一個整數,但即使舍入0.5至最接近的數字如此:
2 = CInt(2.5)
4 = CInt(3.5)
都爲真,這可能不是你想要的。
Math.Round可以告訴從零開始。但返回一倍,所以我們仍然需要投它
3 = CInt(Math.Round(2.5, MidpointRounding.AwayFromZero))
請只問一個問題。如果您有多個問題,請將它們作爲單獨的問題發佈。 –
https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx – CodeCaster
問兩個問題是否有投票原因,或者是否存在其他問題? – AlwaysLearning