136

I am converting a string like "41.00027357629127", and I am using;

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 string should be the same...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mehmet
  • 1,824
  • 3
  • 19
  • 28
  • 2
    How do you know what it is converted to - that is how are you displaying the number – mmmmmm Jun 26 '12 at 08:25
  • 1
    Any idea why its ToSingle? And not ToFloat? – Lord Darth Vader Oct 20 '17 at 10:43
  • 1
    41.00027357629127 shall not be 4.10002732E+15 in scientific notation, unless of course your culture uses decimal comma instead of decimal point (and dot as thousands separator), so the number would actually read: 4100027357629127 consequently become displayed as 4.10002732E+15 – ljgww Oct 05 '18 at 07:45

9 Answers9

271

Your thread's locale is set to one in which the decimal mark is "," instead of ".".

Try using this:

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

Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
34

You can use the following:

float asd = (float) Convert.ToDouble("41.00027357629127");
Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
user4292249
  • 349
  • 3
  • 2
28

First, it is just a presentation of the float number you see in the debugger. The real value is approximately exact (as much as it's possible).

Note: Use always CultureInfo information when dealing with floating point numbers versus strings.

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

This is just an example; choose an appropriate culture for your case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tigran
  • 61,654
  • 8
  • 86
  • 123
8

Use Convert.ToDouble("41.00027357629127");

Convert.ToDouble documentation

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Ozgur Dogus
  • 911
  • 3
  • 14
  • 38
4

The precision of float is 7 digits. If you want to keep the whole lot, you need to use the double type that keeps 15-16 digits. Regarding formatting, look at a post about formatting doubles. And you need to worry about decimal separators in C#.

Community
  • 1
  • 1
jpe
  • 1,013
  • 6
  • 14
  • ı tired double.Parse it returns 4100027357629127.0 it is too bad – Mehmet Jun 26 '12 at 07:43
  • 2
    This sounds like the '.' character is not the decimal separator in your locale? Check out http://stackoverflow.com/questions/3870154/c-sharp-decimal-separator – jpe Jun 26 '12 at 07:48
2

A 2022 way of converting an string that represents a float value:

(float)Convert.ToDecimal(value, CultureInfo.GetCultureInfo("en-US"));

where you also can choose what kind of float are you expecting to convert, because some CultureInfo instances represents decimal values with a , and others with ..

If you need more decimals to obtain more precision, just not use float

Convert.ToDecimal(value, CultureInfo.GetCultureInfo("en-US"));
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29
0

You can double.Parse("41.00027357629127");

ABH
  • 3,391
  • 23
  • 26
-1

First you need to using System.Globalization to dealing convertions from string to float/double/decimal without problem.

Then you can call Parse on float(or double/decimal depending at the accuracy you need), and as argument in Parse you need your string (you can store it in a variable if you want) and CultureInfo.InvariantCulture.NumberFormat

So, as previous users already explained:

float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
Haki
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32416679) – JerodG Aug 10 '22 at 17:23
-3

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

Learner
  • 1,544
  • 8
  • 29
  • 55