By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Zero fill left

P: n/a
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
Apr 3 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Apr 3, 11:22 am, "tshad" <t...@home.comwrote:
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

Apr 3 '07 #2

P: n/a
tshad wrote:
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öran Andersson
_____
http://www.guffa.com
Apr 3 '07 #3

P: n/a
"tshad" <t@home.comwrote in
news:eH**************@TK2MSFTNGP02.phx.gbl:
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.
Apr 3 '07 #4

P: n/a
You can you the padleft and padright functions

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

"tshad" <t@home.comwrote in message
news:eH**************@TK2MSFTNGP02.phx.gbl...
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

Apr 3 '07 #5

P: n/a
"AMDRIT" <am****@hotmail.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 4 '07 #6

P: n/a
why? just curious
"AMDRIT" <am****@hotmail.comschrieb:
>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.

Apr 4 '07 #7

P: n/a
"Jay Parzych" <ji*****@cox.netschrieb:
>>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 4 '07 #8

P: n/a
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 Parzych" <ji*****@cox.netschrieb:
>>>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.

Apr 5 '07 #9

P: n/a
Jay Parzych wrote:
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
Apr 5 '07 #10

P: n/a
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.
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Jay Parzych" <ji*****@cox.netschrieb:
>>>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Apr 5 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.