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 9 2191
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
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
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
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
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.
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
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
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Lisa |
last post: by
|
1 post
views
Thread by Matt Garman |
last post: by
|
7 posts
views
Thread by ilona |
last post: by
|
4 posts
views
Thread by James |
last post: by
|
4 posts
views
Thread by weirdstuff |
last post: by
|
11 posts
views
Thread by Dustan |
last post: by
|
14 posts
views
Thread by Scott M. |
last post: by
|
1 post
views
Thread by schoedl |
last post: by
|
11 posts
views
Thread by coomberjones |
last post: by
| | | | | | | | | | |