Convert string to float in C #

I convert the string as "41.00027357629127" and I use;

Convert.ToSingle("41.00027357629127"); 

or

 float.Parse("41.00027357629127"); 

These methods return 4.10002732E + 15.

When I convert to float, I want "41.00027357629127". This line should be the same ...

+64
string floating-point c # type-conversion
Jun 26 '12 at 7:35
source share
7 answers

Your default locale is set to a decimal place with a,, instead of a..

Try using this:

 float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat); 

Note, however, that a float cannot hold as many precision digits. For this you will have to use double or Decimal.

+138
Jun 26 '12 at 8:01
source share

Firstly, it’s just a presentation of the float number that you see in the debugger. The actual value is approximately as accurate (as possible).

Note. Always use CultureInfo when dealing with floating point numbers versus strings.

 float.Parse("41.00027357629127", System.Globalization.CultureInfo.InvariantCulture); 

This is just an example; Choose the right culture for your occasion.

+18
Jun 26 '12 at 7:40
source share

Use Convert.ToDouble("41.00027357629127");

Convert.ToDouble documentation

+7
Jun 26 2018-12-12T00:
source share

You can use "float asd = (float) Convert.ToDouble("41.00027357629127");

+7
Nov 25 '14 at 15:05
source share

Float accuracy is 7 digits. If you want to save the whole batch, you need to use double , which stores 15-16 digits. Regarding formatting, look at the message about formatting doubling . And you need to worry about decimal separators in C # .

+4
Jun 26 2018-12-12T00:
source share

You can double.Parse("41.00027357629127");

0
Jun 26 2018-12-12T00:
source share

You can use parsing with double instead of float to get a more accurate value.

0
Jun 26 2018-12-12T00:
source share



All Articles