473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Format float

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
7 13069
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
fmt
Thanks for fast reply. I sure will try..

Feb 5 '06 #3
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
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
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
fmt
Got it Jon,
I did
string strReturn=(Rate.ToString("#.00"));
It works.
Thanks

Feb 5 '06 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
16
by: David | last post by:
I have following code. float b; b= 1.234; cout<<hex<<b<<endl; How come it cannot ouput the hex representation of b?
5
by: geskerrett | last post by:
We are working on a project to decipher a record structure of an old accounting system that originates from the late80's mid-90's. We have come across a number format that appears to be a "float"...
3
by: Mahmood Ahmad | last post by:
Hi, I am getting the error 'Input String was not in correct format' in the following C# program (indicated line): using System; namespace CF7 { class CalcArea {
9
by: gamehack | last post by:
Hi all, I've been wondering when I write a structure like: struct { int a; unsigned int b; float c; } mystruct;
7
by: Aek | last post by:
Hi everyone, I am trying to construct a regular expression and format string to use with a boost::regex_replace() In my file the sample text is: // .fx shader file FLOAT JOE 3545f; FLOAT...
12
by: astri | last post by:
i`m doing my thesis comparing CORDIC with polynomial in counting arctan with fixed point. I`m using Q15 format now. I`m using this site CORDIC arctan as a referenced when making with floating...
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
4
by: siddhanta | last post by:
float x = new float; float y = new float; int c=0; using (StreamReader sr = new StreamReader("e: \\CuPeak.dat")) { while (!sr.EndOfStream) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.