141

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...

3
  • 2
    How do you know what it is converted to - that is how are you displaying the number
    – mmmmmm
    Commented Jun 26, 2012 at 8:25
  • 1
    Any idea why its ToSingle? And not ToFloat? Commented Oct 20, 2017 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
    Commented Oct 5, 2018 at 7:45

9 Answers 9

284

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.

1
  • 46
    don't forget to add the namespace using System.Globalization; to access CultureInfo Commented Jan 7, 2014 at 9:12
34

You can use the following:

float asd = (float) Convert.ToDouble("41.00027357629127");
1
  • 5
    Please provide some context as to why this is the right answer. Commented Nov 25, 2014 at 15:20
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.

2
  • how can ı choose my CultureInfo ? I am in turkey where ı will add TR-TR ?
    – Mehmet
    Commented Jun 26, 2012 at 7:55
  • 1
    @Mehmet new System.Globalization.CultureInfo("tr-TR")
    – sshow
    Commented Jun 26, 2012 at 8:04
8

Use Convert.ToDouble("41.00027357629127");

Convert.ToDouble documentation

2
  • 2
    Doesn't this change the datatype from float to double? OP wanted float. Commented Oct 24, 2017 at 14:50
  • ust (float) Convert.ToDouble("41.00027357629127"); to convert to float too.
    – Liam
    Commented Jan 2, 2023 at 13:59
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#.

2
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"));
0

You can double.Parse("41.00027357629127");

1
  • I don't think he's asking how to round it. Commented Jun 26, 2012 at 7:39
-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);
1
-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.