Solved

Padding Left Side Of Number - VB.NET

Posted on 2009-07-13
6
470 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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 140 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 59

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 85

Accepted Solution

by:
Mike Tomlinson earned 260 total points
ID: 24846060
If you are working with STRING values from the DB then use String.PadLeft() or String.PadRight().
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 59

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 100 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 48
VS.net 2010 11 37
SQL LINE CONTINUATION ISSUE 12 33
Help with consolidating excel files using VB.net 2 26
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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