Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

Add leading zeros

Hi all,

I am using VB.NET (VS 2003).

I have a numeric value that I am retrieving from a SQL Server database. This could be anything from 1 to 999.

What I want to do is to always have the value showing as 3 digits in the textbox on my VB form. ie.
3 = 003
11 = 011
97 = 097
100 = 100

Any ideas guys?

Cheers
0
FMabey
Asked:
FMabey
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
one possibility:
yourstring = yourvariable.ToString().LeftPad(3, '0');
0
 
FMabeyAuthor Commented:
angellll,

Cheers for the speedy response.

I have entered the following code:

        Dim STR_id As String
        STR_id = TXT_id.Text
        TXT_test.text = STR_id.ToString().LeftPad(3, '0')

It tells me that 'leftpad is not a member of string'. Any ideas where I'm going wrong?
0
 
jpaulinoCommented:
You can use

Dim value As Integer = 3
MsgBox(Format(value, "000"))
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, my error, the function is called PadLeft():

        Dim STR_id As String
        STR_id = TXT_id.Text
        TXT_test.text = STR_id.ToString().PadLeft(3, '0')
0
 
FMabeyAuthor Commented:
Excellent,

Thanks angellll. Just to make one ammendmant for anyone searching on this. The code I used was this...

        Dim STR_id As String
        STR_id = TXT_id.Text
        TXT_test.text = STR_id.ToString().PadLeft(3, "0")

The only alteration was '0' changed to "0".
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now