Split Number


How can I split a four-digit number into ones, tens, hundreds and thousands ?



  • seancampbellseancampbell Pennsylvania, USA
    There are a few different ways, here is an easy method if the number is always for long

    If you know the number will always be 4 long, you can do String manipulation to it, example:

    Dim I as integer = 2934

    If (I.toString.Length < 4 or I.toString.Length > 4) Then
    'The length of the string is greater than, or less than 4
    'so the number is not 4 digits...
    MsgBox("You need to enter a 4 digit number")
    End If

    Dim Ones as String = I.toString.SubString(3,1)
    'SubString is a function that returns a section
    'of string matching your conditions
    'SubString(Length) returns the rest of a string starting on the
    'point you pass it, SubString(0) is the first character
    'SubString(StartingPoint, Length) can return 1 or more characters
    'starting from where you specify

    'As you can see, Ones = 4

    'It should be pretty simple to figure out the rest of the positions
    Dim Tens as String = I.toString.SubString(,1)
  • Ya it works.

    Thanks very much.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!