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.
 
Back
Top