By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,140 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,140 IT Pros & Developers. It's quick & easy.

string/output formatting

P: n/a
Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?

C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
and hex but these do not seem to allow for specifying the number of
decimals, left/right placement, or string formatting.

Thanks

john

Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
john coltrane <te*********@yahoo.comwrote:
Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?

C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
and hex but these do not seem to allow for specifying the number of
decimals, left/right placement, or string formatting.
Yes, they do.

Look up "standard numeric format strings" and "custom numeric format
strings" in MSDN.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

P: n/a
Number formats will allow codes like these.
C is for currency and will output based on currency formatting options for
machine the code is running on
F is for a fixed point number and allows you to specify the number of
decimal places. e.g for 2 decimal places, write F2
There are others which are documented in the MSDN but here is a good guide
you can print:

http://www.timesonline.co.uk/tol/spo...cle3968859.ece

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"john coltrane" wrote:
Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?

C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
and hex but these do not seem to allow for specifying the number of
decimals, left/right placement, or string formatting.

Thanks

john

Jun 27 '08 #3

P: n/a
Ciaran,
thanks, with printf I can do f10.2 to specify the field length and the
number of decimals. It seems that this functionality does not exist in the
FCL. Also, there does not seem to be a way to specify the field size for a
string. I guess I will have to construct this myself.

john

"Ciaran O''Donnell" <Ci************@discussions.microsoft.comwrote in
message news:2A**********************************@microsof t.com...
Number formats will allow codes like these.
C is for currency and will output based on currency formatting options for
machine the code is running on
F is for a fixed point number and allows you to specify the number of
decimal places. e.g for 2 decimal places, write F2
There are others which are documented in the MSDN but here is a good guide
you can print:

http://www.timesonline.co.uk/tol/spo...cle3968859.ece

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"john coltrane" wrote:
>Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?

C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general,
numerical,
and hex but these do not seem to allow for specifying the number of
decimals, left/right placement, or string formatting.

Thanks

john


Jun 27 '08 #4

P: n/a
I would like to be able to specify the size and number of decimals of a
number and whether the field is left or right justified. Also, there doesn't
seem to be a way to specify the size of a string field. In other words I
would like the functionality of printf formatting.

Numeric formatting, n:10, results in "100.0000000000" instead of "
100".

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
john coltrane <te*********@yahoo.comwrote:
>Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?

C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general,
numerical,
and hex but these do not seem to allow for specifying the number of
decimals, left/right placement, or string formatting.

Yes, they do.

Look up "standard numeric format strings" and "custom numeric format
strings" in MSDN.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Jun 27 '08 #5

P: n/a
Or maybe just create a little c dll that would call printf for you, and you
can call that from c#.

...ab

"john coltrane" <te*********@yahoo.comwrote in message
news:Oa**************@TK2MSFTNGP03.phx.gbl...
string. I guess I will have to construct this myself.
Jun 27 '08 #6

P: n/a
Abubakar wrote:
Or maybe just create a little c dll that would call printf for you, and
you can call that from c#.
Don't do this -- performance hit, unnecessarily requiring P/Invoke
permissions, buffer overflow concerns, additional build targets and last and
in this case least an extra binary to deploy. In short, this is inferior to
learning how formatting works in C# (and how you can extend the mechanism
yourself) in just about every way except possibly development time, but I'd
consider it an investment.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #7

P: n/a
john coltrane <te*********@yahoo.comwrote:
I would like to be able to specify the size and number of decimals of a
number and whether the field is left or right justified. Also, there doesn't
seem to be a way to specify the size of a string field. In other words I
would like the functionality of printf formatting.

Numeric formatting, n:10, results in "100.0000000000" instead of "
100".
You shouldn't give up just because your first attempt didn't do what
you wanted to (although it *did* do what the documentation stated).
But:

If you want " 100":
Console.WriteLine("{0,10:N0}", 100);

Left aligned string, expanded to 5 chars:
Console.WriteLine("{0,-5}", "abc");

Right aligned string, expanded to 5 chars:
Console.WriteLine("{0,5}", "abc");

I can't immediately see a way of making it trim as well as expanding,
admittedly - but it's not clear whether or not you wanted that.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #8

P: n/a
Jeroen Mostert wrote:
Abubakar wrote:
>Or maybe just create a little c dll that would call printf for you,
and you can call that from c#.
Don't do this -- performance hit, unnecessarily requiring P/Invoke
permissions, buffer overflow concerns, additional build targets and last
and in this case least an extra binary to deploy. In short, this is
inferior to learning how formatting works in C# (and how you can extend
the mechanism yourself) in just about every way except possibly
development time, but I'd consider it an investment.
Thanks Jeroen,
I was hoping that C# or the .Net Framework would provide more robust
formatting functionality. Extending IFormatProvider and/or
ICustomFormatter would involve a lot of work to get to provide the same
functionality as printf. The limited functionality using the format
types C, D, E, F, G, N, P, R, and X just doesn't meet my needs.

oh well
Jun 27 '08 #9

P: n/a
John Coltrane wrote:
I was hoping that C# or the .Net Framework would provide more robust
formatting functionality. Extending IFormatProvider and/or
ICustomFormatter would involve a lot of work to get to provide the same
functionality as printf. The limited functionality using the format
types C, D, E, F, G, N, P, R, and X just doesn't meet my needs.
..NET has excellent formatting capabilities.

But less excellent documentation of them.

Try read:
http://blog.stevex.net/index.php/str...ing-in-csharp/

Arne
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.