Imports System
Public Module NumberUtils
''' <summary>
''' Returns the N-th digit of a number counting from the left.
''' </summary>
''' <param name="number">
''' The input number.
''' </param>
''' <param name="position">
''' The digit position (1-based, from the left).
''' </param>
''' <returns>
''' The digit at the specified position.
''' </returns>
''' <exception cref="ArgumentOutOfRangeException">
''' Thrown when position is out of range.
''' </exception>
Public Function GetDigitFromLeft(number As Long, position As Integer) As Integer
Dim digits As String = Math.Abs(number).ToString()
If position < 1 OrElse position > digits.Length Then
Throw New ArgumentOutOfRangeException(
NameOf(position),
"Position is out of range."
)
End If
Return Integer.Parse(digits(position - 1).ToString())
End Function
Public Sub Main()
Console.WriteLine(GetDigitFromLeft(62345, 1))
Console.WriteLine(GetDigitFromLeft(12845, 3))
End Sub
End Module
' run:
'
' 6
' 8