Formatted strings

  • Thread starter Thread starter Wynn Rostek
  • Start date Start date
W

Wynn Rostek

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
 
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
 
Wynn Rostek said:
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")
 
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
 
Back
Top