7

I want to convert float value to string.

Below is the code which i am using for the conversion.

static void Main(string[] args)
        {
            string s =string.Format("{0:G}", value);                
            Console.Write(s);
            Console.ReadLine();
        }

and it outputs as 2.5

But my problem is i want to get the value as 2.50 because i want to compare it with original value later in my project.

so please suggest me if there are any ways to do it?

Nomesh Gajare
  • 855
  • 2
  • 12
  • 28
  • 1
    Why are you comparing numbers by their string representation and not their value? – Corak Sep 27 '13 at 06:19
  • That is requirement in my project. I have to compare the string(which i read from a file) if they are converted back properly.@Corak – Nomesh Gajare Sep 27 '13 at 06:23

3 Answers3

10

You should be using {0:N2} to format to two decimal places.

string.Format("{0:N2}", 2.50)

For 3 decimal places:

string.Format("{0:N3}", 2.50)

And so on.

You can also store the value in a string this way without worrying about precision and then convert your value where you are testing for comparison as string:

string strDecimalVal = Convert.ToString( 2.5000001);
Marco
  • 22,856
  • 9
  • 75
  • 124
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • This way is great. But if i give 2.500 it should also output as 2.500 – Nomesh Gajare Sep 27 '13 at 06:25
  • See my edit. I have already mentioned how you can keep increasing the decimal width - :) – Sadique Sep 27 '13 at 06:26
  • yes i got that, but if i give 2.50 i want the output to be same. Not 2.500 if i specify {0:N3} – Nomesh Gajare Sep 27 '13 at 06:29
  • Then in that case do not format just convert to string like this: `Convert.ToString( 2.5000001)` and store in your string variable – Sadique Sep 27 '13 at 06:31
  • @NomeshGajare as a float, `2.50` is exactly the same as `2.500` or `2.5`. If you want to difference them you should store them as string literals. Edit: See [@heinzi 's answer](http://stackoverflow.com/a/19044089/1192381). – J.A.I.L. Sep 27 '13 at 06:41
  • Heinzi's answer is correct but floating point comparisons are not guaranteed to work in all case for eg if its Nan.http://stackoverflow.com/questions/19027762/why-convert-toint321-0-0-00004-int321-0-0-00004 – Sadique Sep 27 '13 at 06:45
5

because i want to compare it with original value later in my project.

...then you will need to store the number of decimal places the original value had. Once the value is a float, this information is lost. The float representations of 2.5, 2.50 and 2.500 are exactly the same.

So, basically, you have the following possibilities (in order of preference):

  • Don't do a string comparison between the old and the new value. Convert both values to float and then compare them (with a margin of error since floats are not precise).
  • Store the number of decimal places of the old value and then use myFloat.ToString("F" + numDecimals.ToString()) to convert it to a string.
  • Store the value as a string instead of a float. Obviously, you won't be able to do math on that value.

Alternatively, if you do not insist on using floats, decimals might suit your purpose: The do store the number of significant digits:

decimal x = Decimal.Parse("2.50", CultureInfo.InvariantCulture);
decimal y = Decimal.Parse("2.500", CultureInfo.InvariantCulture);

Console.WriteLine(x.ToString()); // prints 2.50
Console.WriteLine(y.ToString()); // prints 2.500
Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
1

Try this

Console.WriteLine("{0:F2}", 2.50);
Console.WriteLine("{0:0.00}", 2.50);
Console.WriteLine("{0:N2}", 2.50);

Version 1 and 2 are almost similar, but 3 is different. 3 will include number separators when number is large.

For example the following outputs 454,542.50

Console.WriteLine("{0:N2}", 454542.50);

More on MSDN

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189