PC Review


Reply
Thread Tools Rate Thread

Converting to string with preceding zero

 
 
John
Guest
Posts: n/a
 
      5th Sep 2003
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



 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      5th Sep 2003
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" <(E-Mail Removed)> wrote in message
news:%23%(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      5th Sep 2003
"John" <(E-Mail Removed)> schrieb:
> 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"))
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Patrick Steele [MVP]
Guest
Posts: n/a
 
      5th Sep 2003
In article <##(E-Mail Removed)>,
(E-Mail Removed) says...
> 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?


String.Format

http://tinyurl.com/mcnc

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
 
Reply With Quote
 
One Handed Man [ OHM ]
Guest
Posts: n/a
 
      5th Sep 2003
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" <(E-Mail Removed)> wrote in message
news:%23%(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Sep 2003
"John" <(E-Mail Removed)> schrieb
> 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

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      5th Sep 2003
Armin,
Doh!

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

Thanks
Jay

"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "John" <(E-Mail Removed)> schrieb
> > 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to TRIM a string variable (remove preceding and trailing blank =?Utf-8?B?U3RlZmFubyBHYXR0bw==?= Microsoft Windows 2000 3 25th Feb 2005 02:36 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jay B. Harlow [MVP - Outlook] Microsoft C# .NET 7 1st Aug 2003 06:03 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Mikael Jansson Microsoft C# .NET 0 31st Jul 2003 08:42 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jon Skeet Microsoft C# .NET 0 31st Jul 2003 08:38 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Frank Oquendo Microsoft C# .NET 0 31st Jul 2003 08:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 AM.