19

I have a text file,which I use to input information into my application.The problem is that some values are float and sometimes they are null,which is why I get an exception.

        var s = "0.0";
        var f = float.Parse(s);

The code above throws an exception at line 2 "Input string was not in a correct format."

I believe the solution would be the advanced overloads of float.Parse,which include IFormatProvider as a parameter,but I don't know anything about it yet.

How do I parse "0.0"?

Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248

6 Answers6

33

Dot symbol "." is not used as separator (this depends on Culture settings). So if you want to be absolutely sure that dot is parsed correctly you need to write something like this:

CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.CurrencyDecimalSeparator = ".";
avarage = double.Parse("0.0",NumberStyles.Any,ci);
inazaruk
  • 74,247
  • 24
  • 188
  • 156
18

Following works for me:

string stringVal = "0.0";
float floatVal = float.Parse(stringVal , CultureInfo.InvariantCulture.NumberFormat);

The reverse case (works for all countries):

float floatVal = 0.0f;
string stringVal = floatVal.ToString("F1", new CultureInfo("en-US").NumberFormat);
9

You can check for null or empty string first.

You can also use one of the overloads of Parse (or even use TryParse) to give more specific control.

E.g. to check using the invarient culture, to avoid decimal separator variations with non-user visible data (e.g. from A2A communications):

float SafeParse(string input) {
  if (String.IsNullOrEmpty(input)) { throw new ArgumentNullException("input"); }

  float res;
  if (Single.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out res)) {
    return res;
  }

  return 0.0f; // Or perhaps throw your own exception type
}
Richard
  • 106,783
  • 21
  • 203
  • 265
3

I've just tried this and it didn't throw any exceptions at all.

Is your number format using decimal comma rather than decimal point? Have you tried:

var s = "0,0";
var f = float.Parse(s);

Having asked this I've just tried it with the comma expecting to get an exception, but didn't. So this might not be the answer.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • The exception can be produced with the right locale, for example english. – Akku Jan 16 '12 at 14:38
  • @Akku - I don't remember the exact test I did (I did post this answer 2 1/2 years ago), but I am aware of locales and how they affect the parsing of numbers. I was suggesting to the user that *his* locale was set to one that expected decimal commas rather than decimal points. – ChrisF Jan 16 '12 at 14:46
  • This [new question](http://stackoverflow.com/questions/8883801/double-tryparse-ignores-numberformatinfo-numbergroupsizes) might explain why I got a result rather than an exception. – ChrisF Jan 16 '12 at 17:28
3

Or, you could simply check if the input text is not null, or empty.

Also, be careful, because in some countries, the "." (dot) that separates the float numbers is "," (comma)

Timotei
  • 1,909
  • 2
  • 22
  • 31
  • Actually,its a dot and it doesn't work.When I changed it to a comma,it worked.I may have to change the whole file. – Ivan Prodanov Jun 18 '09 at 19:03
  • 1
    No, you don't. You can setup dot to be recognized as custom separator. – inazaruk Jun 18 '09 at 19:04
  • You can change your locale from Control Panel-Regional And Language Options. Or the best: change the CultureInfo in the whol application, like Ujn told. – Timotei Jun 19 '09 at 05:37
3

this should work.

var s = "0.0"; var f = float.Parse(s, CultureInfo.InvariantCulture);

wlodi54
  • 31
  • 2
  • 2
    HI Wlodi54 it would be nice if you could explain why that works :) more info [How to answer](https://stackoverflow.com/help/how-to-answer) – Jesse de gans Apr 21 '20 at 10:43