string/output formatting

J

john coltrane

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
 
J

Jon Skeet [C# MVP]

john coltrane said:
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.
 
J

john coltrane

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
 
J

john coltrane

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".
 
A

Abubakar

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

...ab
 
J

Jeroen Mostert

Abubakar said:
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

Jon Skeet [C# MVP]

john coltrane said:
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.
 
J

John Coltrane

Jeroen said:
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
 
A

Arne Vajhøj

John said:
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/string-formatting-in-csharp/

Arne
 

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

Top