473,520 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7490
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3270
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' 2' Now, with a mapping....
0
2278
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It...
6
3385
by: Servé Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname); // "%s"
2
1860
by: ramonred | last post by:
Hi, I am having trouble with a little piece of code that formats an SQL string. I am not looking for folks to debug my code, what I would like to know is how can I see the string that I've built before it is executed, so I can fix whatever is wrong. //snippet ....lots of code building the string sql = String.Format(sql,...
1
4195
by: Chris Morse | last post by:
Hi, I've been trying to figure out where in the documentation it describes all the String.Format() formatting specifiers. So far, I've been guessing and picking up specifiers in sample code.. but I've never seen a detailed descriptions of all that's possible. For example, Dim n As Integer = 65536
5
2306
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
3
12780
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
7
3053
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
1
4188
by: (2b|!2b)==? | last post by:
I have the following line in my code, which is supposed to skip commas and white space and to read a comma seperated value string into the appropriate variables: sscanf(temp.c_str(), "%f%*,%d%*,%f%*,%d%*,%d%*,%f%*,%f%*,%f%*,%f%*,%d%*,%f%*,%d%*,%d", &dt, &ti, &lsp, &lst, &lsv, &o, &h, &l, &bd, &bv, &ak, &av, &cv) ; I suspect the format...
0
7201
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3282
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3279
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1646
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.