Solved

Padding Left Side Of Number - VB.NET

Posted on 2009-07-13
6
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 60

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 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 260 total points
ID: 24846060
If you are working with STRING values from the DB then use String.PadLeft() or String.PadRight().
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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