tags:

views:

68

answers:

3

Converting an object .tostring() removes the leading zeros. The object is not a fixed length, so I can't do object.tostring("0000000") where the number of zeros represents the fixed length.

An example object value is "0357" when I convert that object .tostring it becomes "357".

Is there a method for keeping the leading zeros where the length is not known?

+1  A: 
object.ToString("D7")

Where 7 represents the number of digits to fill.

357 == 0000357

MSDN reference

Dustin Laine
It's not a fixed length as mentioned in the original post.
s15199d
Should the result be fixed length? If so then this will work fine regardless of the length of the initial string. This will make whatever the initial string is to 7 chars, or whatever you specify. Are you trying to always add a fixed amount of zeros to the front?
Dustin Laine
The result is not and should not be a fixed length.
s15199d
If the object's value is "0357" the output should be "0357" if the object's value is "00357" the output should be "00357".
s15199d
@s15199d - If (value s/b 4 digits) Then object.ToString("D4") ElseIf (value s/b 5 digits) object.ToString("D5") etc. I'm really not certain how the value can be stored without knowing how long it should be.
Jason Berkan
If that is it, why not juse prepend the "0" to it, you are not really padding at all. `s = "0" + s;`
Dustin Laine
A: 

Use String.Format method: http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

Dim formatString As String = String.Format("{0:0000}", value)
RBW_IN
A: 

The issue was not .tostring()...I was opening the resulting dataset (a *.csv file) in Excel...Excel was truncating the leading zeros.

s15199d
If you put an apostrophe, ', before the number-string in the csv file then Excel treats the remainder as text so '00356,'00123,'0123 will preserve the leading zeros and keep them as text.
RobS