Formatted strings

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
 
A

Andy

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
 
B

Ben Voigt

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")
 
R

Rad [Visual C# MVP]

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
 

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

Similar Threads

string/output formatting 9
Formatting Strings 1
Converting from C++ to C# 3
get names of parameters 4
Get Strings 3
Significant digits in double with Format strings? 7
Formatting a Floating Point String 2
Round 3

Top