Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Padding Left Side Of Number - VB.NET

Posted on 2009-07-13
6
Medium Priority
?
500 Views
Last Modified: 2012-05-07
Hi,

I have a database field to represent in fixed length - Say length (7). If i have a value "123" from database then i want to represent it with "0000123". Similarly for "1234", i want to represent it with "0001234". Can you please provide me simple function which will do this task in VB.NET ?

Thanks
0
Comment
Question by:milani_lucie
  • 3
  • 2
6 Comments
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 420 total points
ID: 24845970
One method is using ToString with format string like "0000000" or "#0000000".  Alternatively, you could do this on the database end by converting the INT to varchar and then concatenting '0000000' in front of it and then taking the right 7 characters.
Dim i As Integer = 123
MsgBox(i.ToString("0000000"))

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24845983
The VB.NET way more quickly adapts to numbers greater than 7 characters if you ever breach the limits of the 7 digits.  However, here is the SQL version (MS SQL example).
SELECT RIGHT('0000000' + CAST(123 AS VARCHAR(10)), 7);

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 780 total points
ID: 24846060
If you are working with STRING values from the DB then use String.PadLeft() or String.PadRight().
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24846201
Are those still around?  Nice suggestion, Idle Mind.  I use to love using those functions think was in MS Access then Microsoft decided to get rid of them for whatever reason.  Forgot they are now back, or maybe I was misinformed all these years. ;)
0
 
LVL 4

Assisted Solution

by:barrislb
barrislb earned 300 total points
ID: 24847303
Hi milani_lucie,

I'm just providing some sample code to what Idle Mind suggested. I'm a little late in, but nevertheless, I still can assist.  :- )

mwvisa1: yes,  the pad methods are still around. :- ) The PadLeft and PadRight, actually replaced the LSet() and RSet() functions of VB 6.
Dim PadString As String = "1234".PadLeft(7, "0")
MessageBox.Show(RightPadString)

Open in new window

0
 
LVL 4

Expert Comment

by:barrislb
ID: 24847334
MessageBox.Show(RightPadString)  Should Be: MessageBox.Show(PadString)




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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question