How to zero-pad a number?
-
A padded number in a file name!
// Set to padded number
int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;I think this should win an award for readability in a triply-nested ternary condition :rolleyes:.
-
A padded number in a file name!
// Set to padded number
int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;I think this should win an award for readability in a triply-nested ternary condition :rolleyes:.
And what about when someone deletes
085.csv
? :-Dsperamus in juniperus
-
And what about when someone deletes
085.csv
? :-Dsperamus in juniperus
-
A padded number in a file name!
// Set to padded number
int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;I think this should win an award for readability in a triply-nested ternary condition :rolleyes:.
It's so good to know that the hard work of the developers of String.PadLeft[^] and Int32.ToString[^] is providing so much joy to so many, other than you. By the way, what language is that? It can't be C#, or did you also override the
+
operator? :omg: -
It's so good to know that the hard work of the developers of String.PadLeft[^] and Int32.ToString[^] is providing so much joy to so many, other than you. By the way, what language is that? It can't be C#, or did you also override the
+
operator? :omg:PIEBALDconsult wrote:
It can't be C#
Really? ;P
http://msdn.microsoft.com/en-GB/library/k1a63xkz.aspx[^]
When one or both operands are of type string, + concatenates the string representations of the operands.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
PIEBALDconsult wrote:
It can't be C#
Really? ;P
http://msdn.microsoft.com/en-GB/library/k1a63xkz.aspx[^]
When one or both operands are of type string, + concatenates the string representations of the operands.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
When one or both operands are of type string
Yes, and are they?
-
Richard Deeming wrote:
When one or both operands are of type string
Yes, and are they?
PIEBALDconsult wrote:
Yes, and are they?
Yes, the first operand is a string. It's the result of the ternary operator:
fileCount <= 999
? fileCount <= 99
? fileCount <= 9
? "000"
: "00"
: "0"
: ""
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
PIEBALDconsult wrote:
Yes, and are they?
Yes, the first operand is a string. It's the result of the ternary operator:
fileCount <= 999
? fileCount <= 99
? fileCount <= 9
? "000"
: "00"
: "0"
: ""
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
And the second?
-
It's so good to know that the hard work of the developers of String.PadLeft[^] and Int32.ToString[^] is providing so much joy to so many, other than you. By the way, what language is that? It can't be C#, or did you also override the
+
operator? :omg: -
And the second?
PIEBALDconsult wrote:
And the second?
Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
PIEBALDconsult wrote:
And the second?
Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I missed that the first time also. :)
BDF The internet makes dumb people dumber and clever people cleverer. -- PaulowniaK
-
PIEBALDconsult wrote:
And the second?
Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Where do you see that in the documentation? I don't even see
+
in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way. -
Where do you see that in the documentation? I don't even see
+
in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.It's the link that I posted in my previous message: http://msdn.microsoft.com/en-GB/library/k1a63xkz.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Where do you see that in the documentation? I don't even see
+
in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.PIEBALDconsult wrote:
I'm fairly sure that no self-respecting C# professional uses it that way.
Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString(). Like for:
int i = 124, j = 253;
string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
string t = "JD" + (i + j);But yeah, usually I don't bother. Although, theoretically, I could now do
string s = 0; // won't initialise
string t = "" + 0; // will work fine O.oSo, let's get back to self-respecting... :rolleyes:
-
Where do you see that in the documentation? I don't even see
+
in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.PIEBALDconsult wrote:
I'm fairly sure that no self-respecting C# professional uses it that way.
Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString() to clarify.
int i = 124, j = 253;
string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
string t = "JD" + (i + j);But yeah, usually I don't bother. Although, theoretically, I could now do
string s = 0; // won't initialise
string t = "" + 0; // will work fine O.oSo, let's get back to self-respecting... :rolleyes:
-
PIEBALDconsult wrote:
I'm fairly sure that no self-respecting C# professional uses it that way.
Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString() to clarify.
int i = 124, j = 253;
string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
string t = "JD" + (i + j);But yeah, usually I don't bother. Although, theoretically, I could now do
string s = 0; // won't initialise
string t = "" + 0; // will work fine O.oSo, let's get back to self-respecting... :rolleyes:
string t = System.String.Format ( "JD{0}" , i + j ) ;
-
string t = System.String.Format ( "JD{0}" , i + j ) ;
Make that:
string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);
You typically don't want culture-specific formatting or group separators in the result.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Make that:
string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);
You typically don't want culture-specific formatting or group separators in the result.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I've never had that problem.
-
Make that:
string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);
You typically don't want culture-specific formatting or group separators in the result.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A padded number in a file name!
// Set to padded number
int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;I think this should win an award for readability in a triply-nested ternary condition :rolleyes:.