Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float to String Conversion

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?

like image 854
Nomesh Gajare Avatar asked Sep 27 '13 06:09

Nomesh Gajare


People also ask

Can you convert float to string C++?

We can convert float and double to string using the C++11 std::to_string() function. For the older C++ compilers, we can use std::stringstream objects.

How do I convert a float to a string in Python?

In Python, we can use str() to convert float to String.

Can we convert string to float in java?

We can convert String to float in java using Float. parseFloat() method.

How does float to string work?

We can convert a float to String using any of the following two methods: 1) Method 1: Using String. valueOf(float f): We pass the float value to this method as an argument and it returns the string representation of it.

How to convert float to string in C++?

Use the std::to_string () Method to Convert a Float to a String in C++. The to_string function is defined in <string> header and can convert various numeric types to a string value. The method takes a numeric value as a parameter and returns std::string value. Note that, to_string may return unexpected results as the number of significant ...

How to convert float to string in pandas?

There are three methods to convert Float to String: Method 1: Using DataFrame.astype (). This is used to cast a pandas object to a specified dtype.

How to convert float list to string in Python?

We can also notice that the conversion follows the list as stated above “Float -> List -> String.” Here the str method is used to convert to the string data type. The join method takes all the methods in iterable and joins them to make a string. To perform this process this operation, the following methods are used.

What is the difference between float and string in Java?

Float is a datatype that holds floating-point values whereas String is a sequence of characters and a class in Java. To convert Float to String there are several ways like the valueOf () method of String class or toString () method of Float class or a simple string literal that converts an expression to string.


Video Answer


3 Answers

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);
like image 53
Sadique Avatar answered Oct 05 '22 12:10

Sadique


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
like image 39
Heinzi Avatar answered Oct 05 '22 12:10

Heinzi


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

like image 24
Sriram Sakthivel Avatar answered Oct 05 '22 13:10

Sriram Sakthivel