473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting to string with preceding zero

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

Nov 20 '05 #1
6 11945
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" <jo**@nospam.infovis.co.uk> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2
In article <##**************@tk2msftngp13.phx.gbl>,
jo**@nospam.infovis.co.uk 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
Nov 20 '05 #3
"John" <jo**@nospam.infovis.co.uk> 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
Nov 20 '05 #4
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" <jo**@nospam.infovis.co.uk> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #5
"John" <jo**@nospam.infovis.co.uk> 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

Nov 20 '05 #6
Armin,
Doh!

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

Thanks
Jay

"Armin Zingler" <az*******@freenet.de> wrote in message
news:eT**************@tk2msftngp13.phx.gbl...
"John" <jo**@nospam.infovis.co.uk> 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

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mathieu Malaterre | last post by:
Hello, I guess this might be a very dummy question, but I couldn't find an answer in the group's archive. I am reading DICOM files, and to store the string read I use std::string....
15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
4
by: Tobias Müller | last post by:
Hello everybody, I've got some weather data from my local wx station and want to display this as a short table by using a XSL template. The data looks like <?xml version="1.0"?>...
6
by: Mark | last post by:
Hi, My page has a table with many columns such that the right-side of the table gets chopped off when printed. I specify a table width of 100%, but otherwise no cell dimensions are specified. ...
14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
7
by: MeganTSU | last post by:
Hey yall! I am trying to get this program finished for class.... It says that you are suppposed to write a program that will display a check formatted out put (the output looks like a check). I got...
9
by: anupamjain | last post by:
Hi, After 2 weeks of search/hit-and-trial I finally thought to revert to the group to find solution to my problem.(something I should have done much earlier) This is the deal : On a JSP...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: rnakawat | last post by:
I seem to have a problem using the preceding:: on substrings of the value. I'll try to explain with an example: Let's say I had a XML that look like: <Root> <Row> <Cell Name="Street"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.