C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: We use Math.Truncate and print the results to the console with Console.WriteLine.
ConsoleThen: We use implicit casts to Integer. For small doubles (which fit in the integer bytes) the result is the same as Math.Truncate.
IntegerVB.NET program that uses Math.Truncate, Double to Integer casts
Module Module1
Sub Main()
Dim value1 As Double = 1.234
Dim value2 As Double = 1.999
Dim value3 As Double = -1.999
Dim value4 As Double = -1.234
Console.WriteLine(":::TRUNCATE:::")
' Use Math.Truncate to truncate the double values.
Console.WriteLine(Math.Truncate(value1))
Console.WriteLine(Math.Truncate(value2))
Console.WriteLine(Math.Truncate(value3))
Console.WriteLine(Math.Truncate(value4))
Console.WriteLine(":::CAST:::")
' Use implicit Integer casts to truncate the double values.
Dim value1Cast As Integer = value1
Dim value2Cast As Integer = value2
Dim value3Cast As Integer = value3
Dim value4Cast As Integer = value4
Console.WriteLine(value1Cast)
Console.WriteLine(value2Cast)
Console.WriteLine(value3Cast)
Console.WriteLine(value4Cast)
End Sub
End Module
Output
:::TRUNCATE:::
1
1
-1
-1
:::CAST:::
1
2
-2
-1
So: In most cases Math.Truncate is a better choice. It also makes explicit the goal of the operation.