Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 118,421 times

Contents

Related Categories

String Functions - Numbers to Strings

Numbers to Strings

To convert any number to a string, you can use the Str Function. The Str Function uses the following syntax

Str$(Number)

**** Note the $ that has been appended to the Str function name (and many other string functions) You do not have to do this, but if you don't, it will return a Variant rather than a string... which is another VB behavour I can't understand, as we are specifically wanting a string! VB will automatically convert this to a string, but at a performance cost (not noticable if you just do it once, but it is a good habit to get into!). You can see our Performance article for more info. ***

The following code converts the number contained in lNum, and puts it into a textbox

Dim lNum As Long
lNum = 1000
Text1.Text = Str$(lNum)

In many cases it is not necessary to convert a number to a string, as VB automatically does this for you, however it is a good practice to get into, and it is also slightly faster using the Str function, instead of letting VB do it for you. You can also use the Hex command to convert a number to a Hex string. To do this properly, use the following code. This makes all the letters lowercase, and then adds &H on to the beginning, making it a valid Hex string

strHexString = "H&" & LCase$(Hex$(lNumber))

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [69] String Functions

    Posted by KöllMorgan on 14 Jan 2007

    Here we go again:


    After hours of debugging, I found out the only instance the first word on the second line does not get picked up is when I physically tab or hit enter and enter or type ad...

  • Re: [69] String Functions

    Posted by KöllMorgan on 14 Jan 2007

    Replying to my own post...Hiya!


    I found out the trick to grabbing the first word in the string using the above code:


    InStr(1," " & YourString & " ", " " & YourSearc...

  • Re: [69] String Functions

    Posted by KöllMorgan on 13 Jan 2007

    Hello!


     


    Wonderful, helpful site, by the way...


     


    I am working on a small program for a friend and noticed the below code.  Works flawlessly, accept t...

  • Posted by koti_sastry on 06 Dec 2005

    i know i am answering to an oldest question left unanswered..

    but.. i just wanted to answer it..

    just for some one who is searching on a specific topic ...and end up in finding only question...

  • Another way of removing excess spaces

    Posted by fatwalrus on 17 Feb 2005

    [b]I included a code extract here but you have to click on "read comments" to see it all.[/b]

    [i]Basically it just searches the text string for 2 spaces in row, and replaces it with one space, the...