KoolKabin
KoolKabin

Reputation: 17653

How can i get 0 in front of any number?

I would like to append 0 before a number if it is single digit. For example it should be 01,02,03... 09, 10, 11, ...

Upvotes: 26

Views: 73088

Answers (5)

Jerod Venema
Jerod Venema

Reputation: 44632

Try this:

myNum.ToString().PadLeft(2, "0");

Upvotes: 14

MarkJ
MarkJ

Reputation: 30398

Old school method from VB6, still works:

Dim yourNumber as Long = 5 
Format(yourNumber, "00") ' = "05" '

... just for old time's sake :). Better to use Tim's answer.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460098

Dim yourNumber as Int32 = 5
yourNumber.ToString("D2") '= "05"

Upvotes: 36

Lance
Lance

Reputation: 5800

Try the following...

Dim varNumber As Integer = 3
Dim number As String = String.Format("{0:0#}", varNumber)

Hope that helps.

Upvotes: 4

Mattias
Mattias

Reputation: 2306

if(number < 10){
  number = Int32.Parse("0" + number.ToString());
}

I guess that was some c# going on :) but you should get the idea.

Upvotes: -3

Related Questions