Formatting a Floating Point String

  • Thread starter Thread starter ngn
  • Start date Start date
N

ngn

How do you format the number of decimal places in a
floating point number converted to a string?

I simply use the ToString() method on the float variable,
but I cannot find how to format it a #.## format (only 2
places of decimal).

float n = 0.12348528f
string str = n.ToString();
// ??? How do I now format str ???
 
ngn said:
How do you format the number of decimal places in a
floating point number converted to a string?

I simply use the ToString() method on the float variable,
but I cannot find how to format it a #.## format (only 2
places of decimal).

float n = 0.12348528f
string str = n.ToString();
// ??? How do I now format str ???

string str = n.ToString("g2");

Have a look in MSDN for "standard numeric format strings" for more
information.
 

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

Back
Top