Converting to string with preceding zero

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I would like to convert a number to string but with a preceding zero if the
number is less than 10.

How can I accomplish this?

Thanks

Regards
 
John,
What kind of number: Integer or Float?

For Integer (Byte, Short, Integer, Long) you can use the "D" format
specifier, with the number of digits to display:

Dim i as Integer = 9
Dim s As String = i.ToString("D2")

i = 100
s = i.ToString("D2")

For Float (Decimal, Single, Double) you will need to 'manually' do this with
string manipulations (String.PadLeft may help).

Hope this helps
Jay
 
John said:
I would like to convert a number to string but with a preceding zero
if the number is less than 10.

\\\
MsgBox(Format(10, "00"))
MsgBox(Format(9, "00"))
MsgBox(Format(-1, "00"))
MsgBox(Format(-10, "00"))
///
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click



Dim aNumber As Decimal = 10.567D

Dim myString As String

If aNumber < 10 Then

myString = Microsoft.VisualBasic.Format(aNumber, "0.##")

Else

myString = Microsoft.VisualBasic.Format(aNumber, "000.##")

End If

MessageBox.Show(myString)

End Sub


--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
 
John said:
I would like to convert a number to string but with a preceding zero
if the number is less than 10.

How can I accomplish this?

dim i as integer = 5
dim s as string

s = i.tostring("00")
 
Armin,
Doh!

I forgot about the custom format. It will work with Single, Double &
Decimal!

Thanks
Jay
 
Back
Top