Control string and format specifiers

  • Thread starter Thread starter Udi
  • Start date Start date
U

Udi

Hi,
I tried looking for it in the documentation but with no luck.
What's the equivalent C control strings in C# for the following:

"%3d" ? (space padding)
"%-3d" ? (left alignment)
"%+3d" ? (adding +/- sign)
"% 3d" ? (adding a space in case of positive vaklue)

(E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
padding - "%3d"? )

I'll appreciate it if you can direct me to the relevant link.

Thanks!
Udi
 
Udi said:
I tried looking for it in the documentation but with no luck.
What's the equivalent C control strings in C# for the following:

"%3d" ? (space padding)
"%-3d" ? (left alignment)
"%+3d" ? (adding +/- sign)
"% 3d" ? (adding a space in case of positive vaklue)

(E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
padding - "%3d"? )

I'll appreciate it if you can direct me to the relevant link.

If you look up "formatting strings" on MSDN, you'll find the "general"
rules for formatting, which includes alignment. So for the example you
gave, you need {0,3:d} for right alignment, and {0,-3:d} for left
alignment. You can also mix and match: {0,3:d2} will give " 05" for
instance.

Jon
 
Thanks Jon, but this is the link I've looked at, however I didn't see
this notation {0,3:d2} anywhere.
Is there any equivalent for the "u" format specifier?
What if I have a 32 bit value that want to control the way it is
printed with the help of the format specifier just like in printf?
E.g. -
int i = -1;
printf("%u", i);

How do I do it in c#?
Thanks again for your time!
Udi.
 
Udi said:
Thanks Jon, but this is the link I've looked at, however I didn't see
this notation {0,3:d2} anywhere.

Well, it's the {index,alignment:formatString} notation - the d2 is the
formatString part, and the 3 is the alignment.
Is there any equivalent for the "u" format specifier?
What if I have a 32 bit value that want to control the way it is
printed with the help of the format specifier just like in printf?
E.g. -
int i = -1;
printf("%u", i);

How do I do it in c#?

Do you mean you want to treat a signed integer as an unsigned integer?
It's not clear to me (not having done any C for a while) what you're
trying to do.

Jon
 
yes, exactly.

say I have
int i = -1;

but I would like to print it as unsigned, meaning - in this case I'd
like WriteLine() to print the value of MAX_INT.
Is there a way to do it through the format specifiers? I.e. forcing the
value to be printed as unsigned?
Thanks!
 
Udi said:
yes, exactly.

say I have
int i = -1;

but I would like to print it as unsigned, meaning - in this case I'd
like WriteLine() to print the value of MAX_INT.
Is there a way to do it through the format specifiers? I.e. forcing the
value to be printed as unsigned?

Not that I know of, but why don't you just cast it to an unsigned int
in the first place?

Console.WriteLine ("{0:d}", (uint)i);

Jon
 
Thanks Jon.
That's what I had in mind, just wanted to see if there's another way of
doing it to save the casting.
Udi.
 

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

Back
Top