簡體   English   中英

在VB.NET中拆分“十進制”

[英]Splitting a 'Decimal' in VB.NET

我有532.016,我只想在VB.NET中獲得532部分。 我怎樣才能做到這一點?

Math.Truncate(myDecimal)

將去除小數部分,僅保留整數部分(同時不更改類型;即,這還將返回參數的類型,即DoubleDecimal )。

將其轉換為整數。

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

'i now contains 532

您可以使用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

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

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

但是,它不適用於負數。 有關完整說明,請參見此堆棧溢出問題。

Decimal.Truncate (或Math.Truncate )確實是您情況下的最佳選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM