Zero fill left

  • Thread starter Thread starter tshad
  • Start date Start date
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
 
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
 
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")
 
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.
 
You can you the padleft and padright functions

dim i as integer = 42
return i.tostring.padleft(10,"0")
 
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.
 
Performance, readability, semantical "correctness" -- however, I have to
admit that it's just my personal preference.
 
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'
 
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
 
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.
 
Back
Top