0

I have 532.016, and I want to get only the 532 part in VB.NET. How can I do that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Web Worm
  • 2,080
  • 12
  • 40
  • 65

4 Answers4

9
Math.Truncate(myDecimal)

will strip away the fractional part, leaving only the integral part (while not altering the type; i. e. this will return the type of the argument as well, be it Double or Decimal).

Joey
  • 344,408
  • 85
  • 689
  • 683
3

Cast it to an Integer.

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

'i now contains 532
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

You could use 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
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
0

Decimal.floor(532.016) will also return 532.

Decimal.Floor rounds down to the closest integer.

However it won't work for negative numbers. See this Stack Overflow question for a complete explanation

Decimal.Truncate (or Math.Truncate) is really the best choice in your case.

Community
  • 1
  • 1
Kraz
  • 6,910
  • 6
  • 42
  • 70