string.Format() Question

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
 
W

William

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
 
M

mikeb

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top