I have 532.016, and I want to get only the 532 part in VB.NET. How can I do that?
Asked
Active
Viewed 2,169 times
4 Answers
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
-
could you please let me know the whole code about how to do that – Web Worm Apr 03 '10 at 20:15
-
The OP was using Decimal, according to the title of the question. – Joey Apr 03 '10 at 20:22
-
@Johannes Rössel - sharper eyes than mine... Answer corrected – Oded Apr 03 '10 at 20:35
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.