467,077 Members | 1,022 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Formatted strings

In C# is there any way to do formatted printing like there is in C? In C, I
would do sprintf(string, "%4.2f",floatvalue) to print a float value with two
decimal places. I have looked around a bit, but have not run across anything
like this in C#, maybe I am just missing something.

Many thanks,

Wynn
Mar 14 '07 #1
  • viewed: 1863
Share:
3 Replies
On Mar 14, 10:42 am, "Wynn Rostek" <wb4...@amsat.orgwrote:
In C# is there any way to do formatted printing like there is in C? In C, I
would do sprintf(string, "%4.2f",floatvalue) to print a float value with two
decimal places. I have looked around a bit, but have not run across anything
like this in C#, maybe I am just missing something.

Many thanks,

Wynn
Check out string.Format

Mar 14 '07 #2

"Wynn Rostek" <wb****@amsat.orgwrote in message
news:45***********************@roadrunner.com...
In C# is there any way to do formatted printing like there is in C? In C,
I would do sprintf(string, "%4.2f",floatvalue) to print a float value with
two decimal places. I have looked around a bit, but have not run across
anything like this in C#, maybe I am just missing something.
I think something like floatvalue.ToString("N2")
>
Many thanks,

Wynn

Mar 14 '07 #3
On Wed, 14 Mar 2007 10:42:58 -0400, Wynn Rostek wrote:
In C# is there any way to do formatted printing like there is in C? In C, I
would do sprintf(string, "%4.2f",floatvalue) to print a float value with two
decimal places. I have looked around a bit, but have not run across anything
like this in C#, maybe I am just missing something.

Many thanks,

Wynn
All native types have a ToString() method, that has an overload that takes
format strings. What you want to investigate is the various format strings
available. For instance

double f =0;
Console.Write(f.ToString("n"));

Will do what you want
--
Bits.Bytes
http://bytes.thinkersroom.com
Mar 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joerg Lehmann | last post: by
7 posts views Thread by Thomas Philips | last post: by
1 post views Thread by Kevin McBrearty | last post: by
22 posts views Thread by Ram Laxman | last post: by
6 posts views Thread by bfowlkes@gmail.com | last post: by
7 posts views Thread by pkirk25 | last post: by
6 posts views Thread by Jojo | last post: by
4 posts views Thread by RossRGK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.