2

I added a variable of type int and I used it to call something like:

x.ToString("0000");

I changed x to type string and now the above is invalid. Is the only way to format x now:

string.Format("{0:0000}",x);

or is there a short-cut?

I am using StringBuilder to build the string, so does the same apply for AppendFormat?

7
  • Not really, this is basically the way to do it. You are formatting a string, no? Then why not use string.Format? Commented Oct 26, 2012 at 13:33
  • You have to parse the string back to a number to be able to apply a new format. A number just has no format and a string is not numeric. Commented Oct 26, 2012 at 13:35
  • The result for string.Format("{0:0000}",x) is not the same for different kinds of x (namely int and string). So I guess you'll not get desired result by using the above expression. Commented Oct 26, 2012 at 13:36
  • @TimSchmelter - That is what I used to do, just not sure if there was a shorter way. Commented Oct 26, 2012 at 13:37
  • 1
    @Xaisoft: Convert objects to strings at the very last place where you want to display or print them, not earlier. Commented Oct 26, 2012 at 13:38

7 Answers 7

3

You can't format the string as you do a numeric value, so if you want to apply numeric formatting to the string you have to parse it to a number first:

Int32.Parse(x).ToString("0000")
Sign up to request clarification or add additional context in comments.

3 Comments

@Xaisoft: Not so pointless, as at least it works... Using string.Format("{0:0000}", x) when x is a string is pointless, as the formatting is ignored. If x contains for example "42" you get the result "42", not "0042".
What I actually meant by pointless is that I would rather just do string.Format than convert it back to an int and then format it.
@Xaisoft: Yes, it's an inefficient and clumsy way to do it, but there is no shortcut as you have converted the value to a string.
2

Unfortunately, this is the best way. Remember that each type has its own ToString method that can be overridden. The int type's ToString allows you to pass a format to format the integer when converted to string. The DateTime is also similar. However, a string type's ToString only returns the string because the source is already a string type. To format a string, you must call string.Format.

Comments

1

No real shortcut.

Int32 doesn't have a concept of how many leading zeros the int should have.

You're doing the correct thing by formatting to string. If you're using that to display stuff, it shouldn't really be an issue ( you still have x if you want to perform numeric operations ).

AppendFormat works like string.Format, but appends to the StringBuilder object it is called on.

Comments

0

No.

You have to convert the string representation of an integer to an actual integer so that it can be correctly formatted with the required number of leading zeros.

If the string is already in that format why do you need to reformat it?

Comments

0

MSDN shows some ways using a specifier: http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx#Y759

Example

x.ToString("G");

This link lists all the format options: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

1 Comment

FYI, there are standard .ToString() for dates also.
0
x.PadLeft(4, '0'); 

would provide the same result as

x.ToString("0000"); 

provided that x is still a number (it's a string so there's no way to be sure of that without at least TryCast()-ing it back.

1 Comment

PadLeft adds spaces I believe, so shouldn't you use: x.PadLeft(4, '0')?
0

Use string interpolation (starting with C# 6).

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

{<interpolationExpression>[,<alignment>][:<formatString>]}

With your example:

$"{x:0000}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.