在VB.NET中拆分'十进制'

时间:2010-04-03 20:12:41

标签: vb.net

我有532.016,我想只获得VB.NET中的532部分。我怎么能这样做?

4 个答案:

答案 0 :(得分:9)

Math.Truncate(myDecimal)

将剥离小数部分,只留下整数部分(而不改变类型;也就是说,这将返回参数的类型,无论是Double还是Decimal )。

答案 1 :(得分:3)

将其转换为整数。

Dim myDec As Decimal
myDecimal = 532.016
Dim i As Integer = Cint(myDecimal)

'i now contains 532

答案 2 :(得分:0)

您可以使用System.Text.RegularExpressions:

Imports System.Text.RegularExpressions 'You need this for "Split"'

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim yourNumber As String = 532.016 'You could write your integer as a string or convert it to a string'
        Dim whatYouWant() As String 'Notice the "(" and ")", these are like an array'

        whatYouWant = Split(yourNumber, ".") 'Split by decimal'
        MsgBox(whatYouWant(0)) 'The number you wanted is before the first decimal, so you want array "(0)", if wanted the number after the decimal the you would write "(1)"'

    End Sub

End Class

答案 3 :(得分:0)

Decimal.floor(532.016)也会返回532。

Decimal.Floor向下舍入到最接近的整数。

然而,它不适用于负数。 See this Stack Overflow question for a complete explanation

Decimal.Truncate(或Math.Truncate)确实是您的最佳选择。