10

In Visual Basic .NET, I have an integer value:

Dim myNum as Integer = 123

How can I convert this to a String as "0123" (have 4 number digits)?

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
le lan
  • 101
  • 1
  • 1
  • 4
  • 1
    possible duplicate of [C# convert int to string with padding zeros?](http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros) – Eric Mar 13 '15 at 08:04
  • The text and the example conflict. `0123` is only 4 characters, but you said you want 5. Can you clarify? – Joel Coehoorn Mar 14 '15 at 03:24
  • @Eric I'm sure this is a duplicate. There has to be another VB.Net number format question somewhere (I'm too lazy to search for it right now). But it's not a duplicated of the linked question. While they are very similar, and even use the same .Net API, it's rare that we should close a VB question for a C# duplicate. – Joel Coehoorn Mar 14 '15 at 03:29

4 Answers4

13

It's not clear from your question whether you want 4 characters or 5. Here's how you'd do it for 5. If you want 4, just drop one of the zeros in the format string.

Dim myNum as Integer = 123
Dim myNumString As String = myNum.ToString("00000")

You can also do this with String.Format():

Dim myNumString As String = String.Format("{0:00000}", myNum)

And now string interpolation:

Dim myNumString As String = $"{myNum:00000}"

Important: Remember, once you do this the value is no longer a number. It becomes text, which means it will behave as text if you try to do things like sorting or arithmetic.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

As far as converting an integer to a string, try simply using a setup like this:

Dim myNum as Integer = 123;
Dim stringValue as String;
stringValue = CStr(myNum);

If you are needing to add leading zeroes to this, you can either append them with a string manually, or you can try writing a macro that goes something like:

If (myNum - (10 * (myNum / 10))) = 5 Then
     stringValue = CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 4 Then
     stringValue = "0" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 3 Then
     stringValue = "00" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 2 Then
     stringValue = "000" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 1 Then
     stringValue = "0000" & CStr(myNum)
Else
     stringValue = "00000"

The (myNum - (10 * (myNum / 10))) is effectively modulus math. Credit: VBA equivalent to Excel's mod function

I did not test this code, so I apologize if it does not work out of the box, but the general idea is above. The above is also assuming numbers like 123, or 1234, etc. You can also try manipulating strings and combining them by testing the length of your newly converted string.

Dim newString as String
If Len(stringValue) = 3 Then
    newString = "00" & stringValue
ElseIf Len(stringValue) = 2 Then
    newString = "000" & stringValue

etc.

Community
  • 1
  • 1
Ambasabi
  • 91
  • 7
0

Here is a very simple way to convert a number to a string and adding leading zeros if the number length is less than 5 digits

Dim myNum as Integer = 123
Dim strNum as String 

strNum = String(5-len(myNum),"0") & cstr(myNum)

debug.print strNum   'This will display 00123
TechSpeed
  • 9
  • 1
-1

try like Tostring() format.

myNum.ToString("00000")
nanda9894
  • 61
  • 5