Zero fill left

T

tshad

Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom
 
R

rowe_newsgroups

Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom

Something like this?

Private Function Foo(ByVal amt As Integer) As String
Dim s As String = amt.ToString()
If s.Length < 10 Then
Return New String("0"c, 10 - s.Length()) & s
Else
Throw New ArgumentOutOfRangeException("amt", "The length
of amt cannot be greater than 10")
End If
End Function

Thanks,

Seth Rowe
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

tshad said:
Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom

Use a custom format string when you convert the value to a string.

Dim answer As Integer = 42
Dim out As String = answer.ToString("0000000000")
 
G

Guest

Is there a way to zero fill a numeric into a string that is a fixed
lengh of 10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?


You can use either the ToString("FormatStringHere") or the String.PadRight
function.
 
A

AMDRIT

You can you the padleft and padright functions

dim i as integer = 42
return i.tostring.padleft(10,"0")
 
H

Herfried K. Wagner [MVP]

AMDRIT said:
You can you the padleft and padright functions

dim i as integer = 42
return i.tostring.padleft(10,"0")

I'd prefer a custom formatting string ('i.ToString(...)') instead of using
'PadLeft' here.
 
H

Herfried K. Wagner [MVP]

Performance, readability, semantical "correctness" -- however, I have to
admit that it's just my personal preference.
 
J

Jay Parzych

sounds good.

just wrote an app that sends fixed field records and its full of padrights

next one i will try the custom formatting way:

Dim answer As Integer = 42
Dim out As String = answer.ToString("0000000000")

and see which one 'feels right'
 
A

Andrew Morton

Jay said:
sounds good.

just wrote an app that sends fixed field records and its full of
padrights
next one i will try the custom formatting way:

Dim answer As Integer = 42
Dim out As String = answer.ToString("0000000000")

and see which one 'feels right'

PadRight has the advantage that it is self-documenting as to the desired
length of the resulting string :)

Andrew
 
A

AMDRIT

The only issue I have with your method is that it only works fine for left
padding numbers and does nothing for left aligning strings for fixed length
text.

I also think that is more readable when you implicitly call LeftPad or
RightPad, the intent is then known.

I do not believe that performance is an issue here. I am sure that format()
and padleft(), padright() are using similar cycles and resources under the
hood.

Semantical "Correctness" is completing a thought and implicitly stating the
understood.

"you, go to the store." is more correct than "Go to the store." as the
ladder has the understood you in it.

I think that int.tostring.padleft(10,"0") is more correct than
int.tostring("0000000000") as the ladder is understood to be padleft.

At the end of the day though, it is all about preference. Certainly a do
while loop can achieve the same results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top