Format float

  • Thread starter Thread starter fmt
  • Start date Start date
F

fmt

Hello,
I have float comming from database as .5. I need it to format to look
like .50. Please advice
Thanks
 
fmt said:
I have float comming from database as .5. I need it to format to look
like .50. Please advice

Use "f2" as the format specifier and format the string. For instance:

using System;

class Test
{
static void Main()
{
float f = 0.5f;
string x = f.ToString ("f2");
Console.WriteLine (x);
}
}
 
Thanks Jon, I tried it but it gives 0.50. I don't want zero before
decimal. I want it to be .50.
 
fmt said:
Thanks Jon, I tried it but it gives 0.50. I don't want zero before
decimal. I want it to be .50.

And always to 2 decimal places? Try ".00" as the format string then.
 
Thanks Jon,
I tried
string strReturn=(Rate.ToString()+".00");
string strReturn=(String.Format("{0:F2}%",Rate));
Both return.0.50...
 
fmt said:
I tried
string strReturn=(Rate.ToString()+".00");
string strReturn=(String.Format("{0:F2}%",Rate));
Both return.0.50...

The first shouldn't - and doesn't on either 1.1 or 2.0 on my box:

using System;

class Test
{
static void Main()
{
string x = 0.5.ToString(".00");
Console.WriteLine (x);
}
}

prints out ".50" with both .NET versions.
 
Back
Top