By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,973 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,973 IT Pros & Developers. It's quick & easy.

Format float

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

Feb 5 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
fmt <si*******@fmtinv.com> wrote:
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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #2

P: n/a
fmt
Thanks for fast reply. I sure will try..

Feb 5 '06 #3

P: n/a
fmt
Thanks Jon, I tried it but it gives 0.50. I don't want zero before
decimal. I want it to be .50.

Feb 5 '06 #4

P: n/a
fmt <si*******@fmtinv.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #5

P: n/a
fmt
Thanks Jon,
I tried
string strReturn=(Rate.ToString()+".00");
string strReturn=(String.Format("{0:F2}%",Rate));
Both return.0.50...

Feb 5 '06 #6

P: n/a
fmt
Got it Jon,
I did
string strReturn=(Rate.ToString("#.00"));
It works.
Thanks

Feb 5 '06 #7

P: n/a
fmt <si*******@fmtinv.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.