Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vb.net format number to String

Tags:

vb.net

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)?

like image 983
le lan Avatar asked Mar 13 '15 07:03

le lan


1 Answers

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.

like image 67
Joel Coehoorn Avatar answered Sep 22 '22 09:09

Joel Coehoorn