string.Format() Question

  • Thread starter Thread starter Roger Helliwell
  • Start date Start date
R

Roger Helliwell

Hello Everyone,

Has anyone found a quick reference for the many string formatting
options for string.Format() ? I'm spending a ridiculous amount of time
trying to write one line of code that would take about 3 seconds in C.
(Sarcasm not intended.)

All I need is a format string to zero pad an integer out to 9 digits.

[C]
int n = 1234;
printf("PU%09", n); // Displays "PU000001234"

[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?

If anyone has a link to the full format string syntax, let me
know--I'll probably run into a similar problem in the future. (MSDN
was no help.)

Many thanks,
Roger
 
Roger said:
[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?

I think you would write:

string pu = string.Format("{0:n9}", n); // How to pad to 9 digits?

Bill
 
Roger said:
Hello Everyone,

Has anyone found a quick reference for the many string formatting
options for string.Format() ? I'm spending a ridiculous amount of time
trying to write one line of code that would take about 3 seconds in C.
(Sarcasm not intended.)

All I need is a format string to zero pad an integer out to 9 digits.

[C]
int n = 1234;
printf("PU%09", n); // Displays "PU000001234"

[C#]
int n = 1234;
string pu = string.Format("{0}", n); // How to pad to 9 digits?

If anyone has a link to the full format string syntax, let me
know--I'll probably run into a similar problem in the future. (MSDN
was no help.)

Information on formatting strings is more difficult to find that it
should be. For formatting numbers search the docs for "custom numeric
format strings".
 
Back
Top