Trying to use Convert.ToInt32 on string value fails

已答覆 Trying to use Convert.ToInt32 on string value fails

  • venerdì 20 aprile 2012 04:08
     
     
    I'm trying to convert a string value, "1,400", to an int using Convert.ToInt32 and am told that Input string was not in correct format.  Is the comma causing the problem?  If so, is the best way around this using Replace to remove the comma?  Thanks in advance.

    Stan Benner

Tutte le risposte

  • venerdì 20 aprile 2012 04:22
     
     Con risposta Contiene codice

    You may, or you could use Int32.TryParse():

                int i = 0;
                if (Int32.TryParse("1,234",System.Globalization.NumberStyles.AllowThousands, System.Globalization.CultureInfo.InvariantCulture, out i))
                {
                    Console.WriteLine(i);
                }

  • venerdì 20 aprile 2012 04:36
     
     Con risposta Contiene codice
    I'm trying to convert a string value, "1,400", to an int using Convert.ToInt32 and am told that Input string was not in correct format.  Is the comma causing the problem?  If so, is the best way around this using Replace to remove the comma?  Thanks in advance.

    Stan Benner

    Hi,

    you can perform like below...

    String toParse = "1,400";
    int num = int.Parse(toParse, NumberStyles.AllowThousands);


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R

  • venerdì 20 aprile 2012 04:42
     
     Con risposta

    Hello,

    use this...........

     string myStr = "123";
        int myParsedInt = Int32.Parse(myStr);
        int myConvertedInt = Convert.ToInt32(myStr);

    you can also refer this link for it.........

    http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

    http://blogs.msdn.com/b/csharpfaq/archive/2004/05/30/how-do-i-cast-a-string-to-an-int-float-double-etc.aspx

    http://www.developerfusion.com/thread/24964/converting-a-string-to-integerc/

    Regards,


    Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights

  • venerdì 20 aprile 2012 07:12
     
     Con risposta

    If you are not working in an English Environment, then set your system culture to an English Environment. 

    In most languages the comma is a decimal separator, not so clever to use with an Int.

    Or like you wrote replace the comma by an empty string.

    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx

    English cultures are 

    "en-UK"
    "en-US"

    but don't use 

    "en-ZA" in that the comma is also a decimal seperator.


    Success
    Cor

  • venerdì 20 aprile 2012 08:35
     
      Contiene codice

    hi,

    For this,u have to use the following code-

    first u have to add the namespace-

    using System.Globalization;

    then try this code-

    string str = "1,400";
     int mystr =int.Parse(str,NumberStyles.AllowThousands);

  • venerdì 20 aprile 2012 10:01
     
     
    Yup. That's why I choose to use the overload with CultureInfo.InvariantCulture, which is guaranteed to use "," as thousand seperator and "." as decimal seperator and not to try luck with the current culture settings.
  • venerdì 20 aprile 2012 10:45
     
     
    Yup. That's why I choose to use the overload with CultureInfo.InvariantCulture, which is guaranteed to use "," as thousand seperator and "." as decimal seperator and not to try luck with the current culture settings.

    By whom, be aware that South African English (a true version of the English Language) uses the comma as thousand separator.

    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx

    The invariant has more to do with the date representation where in the English language in the US is a complete different format used then in the rest of the world.


    Success
    Cor




  • sabato 21 aprile 2012 02:00
     
     

    Humm... I think the whole point of introducing invariant culture is to make anything related to formatting running with the same behavior in every machine regardless of current system culture settings. All settings in this culture is guaranteed to be the same across systems, certainly not limited to the use of date formatting.

    This is an important aspect of .NET runtime to know of if your application is going to run on systems in multiple countries. Always guard internal transformation (by internal I mean it's non-UI and never meant to be displayed to end-users directly) between string and other datatypes with InvariantCulture.

  • sabato 21 aprile 2012 06:15
     
      Contiene codice
    Id do what you suggested (using the replace method on the string)... e.g:

    string string_to_convert = "1,400";
    string_to_convert = string_to_convert.Replace(",", ""); //removes the comma's
    int value = Convert.ToInt32(string_to_convert);

    This will work.

    Personal Website: http://www.xanather.com

  • venerdì 27 aprile 2012 02:36
     
     
    Your code don't handle thousand seperator, so isn't answering the question.