sgmoore
sgmoore

Reputation: 16067

Int.ToString format equivalent to String.Format("{0,9}" , int)

Is there any value of formatString such that:

intValue.ToString(formatString) == String.Format("{0,9}", intValue)

for all positive integer values up to 1 million?

In particular, the output must be 9 characters and padded with spaces at the start.

000000000 will give me the correct length but padded with zeros.

As to why I don't just use String.Format("{0,9}", intValue), I am trying to create a configuration file which a third party program will read and use to create an output file and I cannot change the code in this program. I know the program is written in C# and it is obvious from the current configuration file that it uses ToString().

Upvotes: 3

Views: 933

Answers (1)

Oded
Oded

Reputation: 498914

You can't.

Composite format strings ({0:xxx}) are not supported by any of the ToString overloads, though the standard numeric format strings are supported by the methods that do use composite format strings.

Standard numeric format strings are also supported by the .NET Framework composite formatting feature, which is used by some Write and WriteLine methods of the Console and StreamWriter classes, the String.Format method, and the StringBuilder.AppendFormat method.

Upvotes: 3

Related Questions