none
Input string was not in a correct format RRS feed

  • Question

  • Hi

    I'm having a error with parsing a decimal from a string

    string lclVersion;
    using (StreamReader reader = new StreamReader("version.txt"))
    {
    	lclVersion = reader.ReadLine();
    }
    decimal localVersion = decimal.Parse(lclVersion); // the error is thrown here.

    version.txt only contains one int or double, both as decimal,

    So what exactly is this error?, could that line be improved?,

    Please help.

    Monday, April 9, 2018 7:15 PM

All replies

  • Hi Zuher,

    In my test environment i don't get any error. Wat is the content of version.rxt, wat is the error?

    regards,

    Rinaldo


    Greetings from Amsterdam the Netherlands


    • Edited by Rinaldo1961 Monday, April 9, 2018 7:34 PM name wrong
    Monday, April 9, 2018 7:33 PM
  • version.txt only contains one int or double, both as decimal,

    What do you mean "as decimal"? The file must contain *strings*.

    - Wayne

    Monday, April 9, 2018 7:41 PM
  • It says string is not valid decimal string to convert it into decimal value. Most of problems are in decimal separator. You can use this:

    decimal localVersion = decimal.Parse(lclVersion, System.Globalization.CultureInfo.CurrentCulture);
    or you can change CultureInfo which is valid for values in file. 
    Monday, April 9, 2018 7:42 PM
  • It can be that the separator in the decimal is a , instead of a .

    Greetings from Amsterdam the Netherlands

    Monday, April 9, 2018 7:47 PM
  • I'm having a error with parsing a decimal from a string

    decimal localVersion = decimal.Parse(lclVersion); // the error is thrown here.

    So what exactly is this error?, could that line be improved?,


    To avoid an exception you can use decimal.TryParse instead.

    bool res = Decimal.TryParse(lclVersion, out localVersion);

    Then check the return to see if it failed, and examine the contents of
    lclVersion if it did.

    Decimal.TryParse Method (String, Decimal)
    https://msdn.microsoft.com/en-us/library/9zbda557(v=vs.110).aspx

    - Wayne

    Monday, April 9, 2018 7:49 PM
  • version.txt only contains one int or double, both as decimal,

    What do you mean "as decimal"? The file must contain *strings*.

    - Wayne

    The StreamReader pass the version.txt data as a string "lclVersion" then it pass's it into a demical.

    lclVersion always contain either int or double (version.txt), so there is no such a string, unless "." is counted as a string which its a double.

    Monday, April 9, 2018 7:51 PM
  • It says string is not valid decimal string to convert it into decimal value. Most of problems are in decimal separator. You can use this:

    decimal localVersion = decimal.Parse(lclVersion, System.Globalization.CultureInfo.CurrentCulture);
    or you can change CultureInfo which is valid for values in file. 
    I am so agreeing on this, because some different devices had that working fine.
    I'll be trying this and reply back.
    Monday, April 9, 2018 7:54 PM

  • The StreamReader pass the version.txt data as a string "lclVersion" then it pass's it into a demical.

    lclVersion always contain either int or double (version.txt), so there is no such a string, unless "." is counted as a string which its a double.

    >The StreamReader pass the version.txt data as a string "lclVersion"

    Yes.

    >then it pass's it into a demical.

    No, StreamReader doesn't make it into a decimal. That's what Parse does.

    >lclVersion always contain either int or double (version.txt), so there
    >is no such a string, unless "." is counted as a string which its a double.

    No. lclVersion is a string variable and it *always* contains a string.

    A "text" file always contains textual data, Usually lines of text
    (characters) with each line followed by a newline character. Those
    strings may *represent* numbers, but are not numbers in themselves.
    That's why they have to be converted before being stored in a variable
    other than a string or character. e.g. - An integer variable, a double
    variable, a decimal variable, etc.

    All of these are strings:

    123
    987.1
    123,456
    6,345.00

    If a file contains "decimals" or "integers" then it is a binary file and
    not a text file. Such a file is created by writing numerical variables
    to a (binary) file in their internal, native formats, rather than after
    being converted to strings.

    Your file apparently contains strings *representing* decimal values,
    but not decimal values themselves (from unconverted numeric variables).

    I'm not being critical or picky here. In order to help with problems
    it is necessary to understand clearly and correctly *exactly* what a
    poster is working with - both code and data. I just wanted to clarify
    that you are in fact reading a text file (as described above), and not
    a "binary" file.

    - Wayne

    Monday, April 9, 2018 8:19 PM
  • Hello Zuher Laith,

    Try to write down the value of lclVersion and everything would be clear for us.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 10, 2018 6:48 AM
    Moderator

  • To avoid an exception you can use decimal.TryParse instead.

    bool res = Decimal.TryParse(lclVersion, out localVersion);

    Then check the return to see if it failed, and examine the contents of
    lclVersion if it did.

    Unfortunately, it throws the same error.
    Tuesday, April 10, 2018 4:54 PM
  • Hello Zuher Laith,

    Try to write down the value of lclVersion and everything would be clear for us.

    Best Regards,

    Neil Hu

    it is "1.0" only.

    on different devices culture, it does not work. (comparing from a english operating system to a french one), for the french operating system it won't work, else it works properly.

    what I mean by a french operating system is, the same Windows but with different language.


    • Edited by Zuher Laith Tuesday, April 10, 2018 5:00 PM
    Tuesday, April 10, 2018 4:58 PM
  • It says string is not valid decimal string to convert it into decimal value. Most of problems are in decimal separator. You can use this:

    decimal localVersion = decimal.Parse(lclVersion, System.Globalization.CultureInfo.CurrentCulture);
    or you can change CultureInfo which is valid for values in file. 
    This seems to be not working after I thought it does
    Tuesday, April 10, 2018 4:59 PM
  • You can change CultureInfo because CurrentCulture is current user culture. But there can be set en-US culture which can be better to parse it.

    System.Globalization.CultureInfo.GetCultureInfo("en-US").

    Tuesday, April 10, 2018 7:58 PM