Input string was not in a correct format #2












12















double temp;
temp = (double)Convert.ToDouble("1234.5678");


Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;




An unhandled exception of type
System.FormatException occurred in
mscorlib.dll



Additional information: Input string
was not in a correct format.











share|improve this question




















  • 4





    +1 for hey ladies ;)

    – Armance
    Dec 15 '11 at 17:06
















12















double temp;
temp = (double)Convert.ToDouble("1234.5678");


Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;




An unhandled exception of type
System.FormatException occurred in
mscorlib.dll



Additional information: Input string
was not in a correct format.











share|improve this question




















  • 4





    +1 for hey ladies ;)

    – Armance
    Dec 15 '11 at 17:06














12












12








12


1






double temp;
temp = (double)Convert.ToDouble("1234.5678");


Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;




An unhandled exception of type
System.FormatException occurred in
mscorlib.dll



Additional information: Input string
was not in a correct format.











share|improve this question
















double temp;
temp = (double)Convert.ToDouble("1234.5678");


Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;




An unhandled exception of type
System.FormatException occurred in
mscorlib.dll



Additional information: Input string
was not in a correct format.








c# .net parsing double






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '12 at 20:00









John Saunders

147k22204362




147k22204362










asked Mar 11 '11 at 16:11









Keith LoughnaneKeith Loughnane

1601117




1601117








  • 4





    +1 for hey ladies ;)

    – Armance
    Dec 15 '11 at 17:06














  • 4





    +1 for hey ladies ;)

    – Armance
    Dec 15 '11 at 17:06








4




4





+1 for hey ladies ;)

– Armance
Dec 15 '11 at 17:06





+1 for hey ladies ;)

– Armance
Dec 15 '11 at 17:06












10 Answers
10






active

oldest

votes


















23














As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.






share|improve this answer
























  • Thanks, That was it.

    – Keith Loughnane
    Mar 14 '11 at 13:07



















21














In order to convert string to double without an exception:




An unhandled exception of type System.FormatException occurred in
mscorlib.dll



Additional information: Input string was not in a correct format.




make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:



double.Parse("1234.5678", CultureInfo.InvariantCulture) 





share|improve this answer

































    9














    first solution (as mentioned in other posts):



    double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);


    second solution - make it default to current thread:



    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    double temp = double.Parse("1234.5678");


    third solution - make it default to block of code:



    var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    ...
    double temp = double.Parse("1234.5678");
    ...
    Thread.CurrentThread.CurrentCulture = prevCurrentCulture;





    share|improve this answer































      5














      You may be somehow using a european local. In some countries the . and , in numbers is reversed.






      share|improve this answer































        4














        Hi as Mario says you must parse it taking into account the regional settings.



        double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);


        Regards.






        share|improve this answer































          2














          Check your regional settings. Your decimal symbol needs to be ".".






          share|improve this answer































            0














            double temp = double.Parse("1234,5678");





            share|improve this answer































              0














              I recommend you use TryParse instead, so you don't need to handle parsing exceptions.



              double temp = 0;
              if (double.TryParse("123.456", out temp)
              {
              Console.WriteLine(string.Format("Parsed temp: {0}", temp);
              }
              else
              {
              Console.WriteLine("Input value was not able to be parsed.");
              }





              share|improve this answer































                0














                I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.






                share|improve this answer

































                  0














                  I found the problem when you let the text box empty then
                  this error occurs so try this one to handle it.




                  An unhandled exception of type System.FormatException occurred in
                  mscorlib.dll Additional information: Input string was not in a correct
                  format.




                  if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}





                  share|improve this answer

























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5275380%2finput-string-was-not-in-a-correct-format-2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    10 Answers
                    10






                    active

                    oldest

                    votes








                    10 Answers
                    10






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    23














                    As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.






                    share|improve this answer
























                    • Thanks, That was it.

                      – Keith Loughnane
                      Mar 14 '11 at 13:07
















                    23














                    As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.






                    share|improve this answer
























                    • Thanks, That was it.

                      – Keith Loughnane
                      Mar 14 '11 at 13:07














                    23












                    23








                    23







                    As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.






                    share|improve this answer













                    As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 11 '11 at 16:16









                    MarioMario

                    29.3k44165




                    29.3k44165













                    • Thanks, That was it.

                      – Keith Loughnane
                      Mar 14 '11 at 13:07



















                    • Thanks, That was it.

                      – Keith Loughnane
                      Mar 14 '11 at 13:07

















                    Thanks, That was it.

                    – Keith Loughnane
                    Mar 14 '11 at 13:07





                    Thanks, That was it.

                    – Keith Loughnane
                    Mar 14 '11 at 13:07













                    21














                    In order to convert string to double without an exception:




                    An unhandled exception of type System.FormatException occurred in
                    mscorlib.dll



                    Additional information: Input string was not in a correct format.




                    make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:



                    double.Parse("1234.5678", CultureInfo.InvariantCulture) 





                    share|improve this answer






























                      21














                      In order to convert string to double without an exception:




                      An unhandled exception of type System.FormatException occurred in
                      mscorlib.dll



                      Additional information: Input string was not in a correct format.




                      make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:



                      double.Parse("1234.5678", CultureInfo.InvariantCulture) 





                      share|improve this answer




























                        21












                        21








                        21







                        In order to convert string to double without an exception:




                        An unhandled exception of type System.FormatException occurred in
                        mscorlib.dll



                        Additional information: Input string was not in a correct format.




                        make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:



                        double.Parse("1234.5678", CultureInfo.InvariantCulture) 





                        share|improve this answer















                        In order to convert string to double without an exception:




                        An unhandled exception of type System.FormatException occurred in
                        mscorlib.dll



                        Additional information: Input string was not in a correct format.




                        make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:



                        double.Parse("1234.5678", CultureInfo.InvariantCulture) 






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 28 '13 at 11:52

























                        answered Dec 24 '12 at 19:41









                        Vadim GremyachevVadim Gremyachev

                        35k770113




                        35k770113























                            9














                            first solution (as mentioned in other posts):



                            double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);


                            second solution - make it default to current thread:



                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            double temp = double.Parse("1234.5678");


                            third solution - make it default to block of code:



                            var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            ...
                            double temp = double.Parse("1234.5678");
                            ...
                            Thread.CurrentThread.CurrentCulture = prevCurrentCulture;





                            share|improve this answer




























                              9














                              first solution (as mentioned in other posts):



                              double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);


                              second solution - make it default to current thread:



                              Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                              double temp = double.Parse("1234.5678");


                              third solution - make it default to block of code:



                              var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
                              Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                              ...
                              double temp = double.Parse("1234.5678");
                              ...
                              Thread.CurrentThread.CurrentCulture = prevCurrentCulture;





                              share|improve this answer


























                                9












                                9








                                9







                                first solution (as mentioned in other posts):



                                double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);


                                second solution - make it default to current thread:



                                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                                double temp = double.Parse("1234.5678");


                                third solution - make it default to block of code:



                                var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
                                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                                ...
                                double temp = double.Parse("1234.5678");
                                ...
                                Thread.CurrentThread.CurrentCulture = prevCurrentCulture;





                                share|improve this answer













                                first solution (as mentioned in other posts):



                                double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);


                                second solution - make it default to current thread:



                                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                                double temp = double.Parse("1234.5678");


                                third solution - make it default to block of code:



                                var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
                                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                                ...
                                double temp = double.Parse("1234.5678");
                                ...
                                Thread.CurrentThread.CurrentCulture = prevCurrentCulture;






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 15 '14 at 11:11









                                BachorBachor

                                41079




                                41079























                                    5














                                    You may be somehow using a european local. In some countries the . and , in numbers is reversed.






                                    share|improve this answer




























                                      5














                                      You may be somehow using a european local. In some countries the . and , in numbers is reversed.






                                      share|improve this answer


























                                        5












                                        5








                                        5







                                        You may be somehow using a european local. In some countries the . and , in numbers is reversed.






                                        share|improve this answer













                                        You may be somehow using a european local. In some countries the . and , in numbers is reversed.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 11 '11 at 16:17









                                        Steve WellensSteve Wellens

                                        19.1k22054




                                        19.1k22054























                                            4














                                            Hi as Mario says you must parse it taking into account the regional settings.



                                            double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);


                                            Regards.






                                            share|improve this answer




























                                              4














                                              Hi as Mario says you must parse it taking into account the regional settings.



                                              double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);


                                              Regards.






                                              share|improve this answer


























                                                4












                                                4








                                                4







                                                Hi as Mario says you must parse it taking into account the regional settings.



                                                double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);


                                                Regards.






                                                share|improve this answer













                                                Hi as Mario says you must parse it taking into account the regional settings.



                                                double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);


                                                Regards.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 11 '11 at 16:23









                                                Marco MedranoMarco Medrano

                                                584




                                                584























                                                    2














                                                    Check your regional settings. Your decimal symbol needs to be ".".






                                                    share|improve this answer




























                                                      2














                                                      Check your regional settings. Your decimal symbol needs to be ".".






                                                      share|improve this answer


























                                                        2












                                                        2








                                                        2







                                                        Check your regional settings. Your decimal symbol needs to be ".".






                                                        share|improve this answer













                                                        Check your regional settings. Your decimal symbol needs to be ".".







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Mar 11 '11 at 16:16









                                                        Tertius GeldenhuysTertius Geldenhuys

                                                        352214




                                                        352214























                                                            0














                                                            double temp = double.Parse("1234,5678");





                                                            share|improve this answer




























                                                              0














                                                              double temp = double.Parse("1234,5678");





                                                              share|improve this answer


























                                                                0












                                                                0








                                                                0







                                                                double temp = double.Parse("1234,5678");





                                                                share|improve this answer













                                                                double temp = double.Parse("1234,5678");






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Mar 11 '11 at 16:14









                                                                AchilleterzoAchilleterzo

                                                                686514




                                                                686514























                                                                    0














                                                                    I recommend you use TryParse instead, so you don't need to handle parsing exceptions.



                                                                    double temp = 0;
                                                                    if (double.TryParse("123.456", out temp)
                                                                    {
                                                                    Console.WriteLine(string.Format("Parsed temp: {0}", temp);
                                                                    }
                                                                    else
                                                                    {
                                                                    Console.WriteLine("Input value was not able to be parsed.");
                                                                    }





                                                                    share|improve this answer




























                                                                      0














                                                                      I recommend you use TryParse instead, so you don't need to handle parsing exceptions.



                                                                      double temp = 0;
                                                                      if (double.TryParse("123.456", out temp)
                                                                      {
                                                                      Console.WriteLine(string.Format("Parsed temp: {0}", temp);
                                                                      }
                                                                      else
                                                                      {
                                                                      Console.WriteLine("Input value was not able to be parsed.");
                                                                      }





                                                                      share|improve this answer


























                                                                        0












                                                                        0








                                                                        0







                                                                        I recommend you use TryParse instead, so you don't need to handle parsing exceptions.



                                                                        double temp = 0;
                                                                        if (double.TryParse("123.456", out temp)
                                                                        {
                                                                        Console.WriteLine(string.Format("Parsed temp: {0}", temp);
                                                                        }
                                                                        else
                                                                        {
                                                                        Console.WriteLine("Input value was not able to be parsed.");
                                                                        }





                                                                        share|improve this answer













                                                                        I recommend you use TryParse instead, so you don't need to handle parsing exceptions.



                                                                        double temp = 0;
                                                                        if (double.TryParse("123.456", out temp)
                                                                        {
                                                                        Console.WriteLine(string.Format("Parsed temp: {0}", temp);
                                                                        }
                                                                        else
                                                                        {
                                                                        Console.WriteLine("Input value was not able to be parsed.");
                                                                        }






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Mar 11 '11 at 16:17









                                                                        Mike AtlasMike Atlas

                                                                        7,00333456




                                                                        7,00333456























                                                                            0














                                                                            I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.






                                                                            share|improve this answer






























                                                                              0














                                                                              I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.






                                                                              share|improve this answer




























                                                                                0












                                                                                0








                                                                                0







                                                                                I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.






                                                                                share|improve this answer















                                                                                I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.







                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Mar 11 '11 at 16:20

























                                                                                answered Mar 11 '11 at 16:15









                                                                                KumarKumar

                                                                                97447




                                                                                97447























                                                                                    0














                                                                                    I found the problem when you let the text box empty then
                                                                                    this error occurs so try this one to handle it.




                                                                                    An unhandled exception of type System.FormatException occurred in
                                                                                    mscorlib.dll Additional information: Input string was not in a correct
                                                                                    format.




                                                                                    if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}





                                                                                    share|improve this answer






























                                                                                      0














                                                                                      I found the problem when you let the text box empty then
                                                                                      this error occurs so try this one to handle it.




                                                                                      An unhandled exception of type System.FormatException occurred in
                                                                                      mscorlib.dll Additional information: Input string was not in a correct
                                                                                      format.




                                                                                      if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}





                                                                                      share|improve this answer




























                                                                                        0












                                                                                        0








                                                                                        0







                                                                                        I found the problem when you let the text box empty then
                                                                                        this error occurs so try this one to handle it.




                                                                                        An unhandled exception of type System.FormatException occurred in
                                                                                        mscorlib.dll Additional information: Input string was not in a correct
                                                                                        format.




                                                                                        if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}





                                                                                        share|improve this answer















                                                                                        I found the problem when you let the text box empty then
                                                                                        this error occurs so try this one to handle it.




                                                                                        An unhandled exception of type System.FormatException occurred in
                                                                                        mscorlib.dll Additional information: Input string was not in a correct
                                                                                        format.




                                                                                        if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}






                                                                                        share|improve this answer














                                                                                        share|improve this answer



                                                                                        share|improve this answer








                                                                                        edited Nov 22 '18 at 9:04









                                                                                        Moritz

                                                                                        57.6k19131184




                                                                                        57.6k19131184










                                                                                        answered Nov 22 '18 at 8:58









                                                                                        Tahir ShahzadTahir Shahzad

                                                                                        1




                                                                                        1






























                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5275380%2finput-string-was-not-in-a-correct-format-2%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            Post as a guest















                                                                                            Required, but never shown





















































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown

































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown







                                                                                            Popular posts from this blog

                                                                                            Tonle Sap (See)

                                                                                            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                                                                                            Guatemaltekische Davis-Cup-Mannschaft