Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert / Format Numeric to String, Suppress Lead Zeros 1

Status
Not open for further replies.

AustinOne

Programmer
Mar 22, 2002
59
US
Simple question from an old COBOLer / VB newbie:

What is a concise way to convert an integer to a string such that the resulting value in the string is 4 characters long, right adjusted with leading zeros suppressed (converted to spaces)?

The value will vary from 1 to 9999, and I want it to appear right-adjusted. For example, 1 would appear as bbb1 and 10 would appear as bb10, where the b represents a space.

FYI, here's the current statement that I am playing with (which isn't doing this and will need to be changed to reflect your answer):

Dim lvi As ListViewItem = _
ListView1.Items.Add((i + 1).ToString("D4"), 0)

In COBOL, you would have A defined as PIC 9(4) and B defined as PIC ZZZ9 or Z(4), and you would just MOVE A TO B or you would use an INSPECT statement, etc.

I have searched for the answer and haven't found it.

Thanks,

AustinOne
 
dim i as integer = 1
i.tostring(" ")

where I have 4 spaces between the quotes

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Thanks, but that didn't work for me. Based on my understanding of what you were saying, I changed original statement to:
Dim lvi As ListViewItem = _
ListView1.Items.Add((i + 1).ToString(" "), 0)

But now it's just resulting in all spaces.
 
That worked!

My statement now reads:

Dim lvi As ListViewItem = _
ListView1.Items.Add((i + 1).ToString.Padleft(4," "), 0)

And it produces the desired result.

Thanks all!

AustinOne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top