471,325 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Control string and format specifiers

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

Dec 6 '05 #1
6 7350
Udi wrote:
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

Dec 6 '05 #2
Udi
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.

Dec 6 '05 #3
Udi wrote:
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

Dec 6 '05 #4
Udi
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!

Dec 6 '05 #5
Udi wrote:
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

Dec 6 '05 #6
Udi
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.

Dec 6 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Pierre Fortin | last post: by
reply views Thread by Josiah Carlson | last post: by
6 posts views Thread by Servé Lau | last post: by
2 posts views Thread by ramonred | last post: by
1 post views Thread by Chris Morse | last post: by
5 posts views Thread by AMP | last post: by
3 posts views Thread by stathisgotsis | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.