how to convert this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Praveen

    how to convert this

    got a string a="2.3944864845 405107E+18"

    how to convert this into a valid number. which method to use..
    I was using Convert.ToInt64 (a); which is going to exception.


    thanks
    Praveen


  • Peter Morris

    #2
    Re: how to convert this

    try decimal.Parse() instead


    Comment

    • Bjorn Brox

      #3
      Re: how to convert this

      Praveen skrev:
      got a string a="2.3944864845 405107E+18"
      >
      how to convert this into a valid number. which method to use..
      I was using Convert.ToInt64 (a); which is going to exception.
      >
      >
      First: The string you have is not representing an integer, so you have
      to go through a string to double step first.

      Secondly: Converting a string to a double is language dependent, - in
      German, Norwegian and French as an example the expected decimal
      separator is a comma ',', instead of a period (.).

      If you know that the string always is using '.' as decimal point you
      should parse it by using CultureInfo.Inv ariantCulture to avoid a crash
      if running on a German OS.

      Int64 value = Convert.ToInt64 (Convert.ToDoub le(a,
      System.Globaliz ation.CultureIn fo.InvariantCul ture));

      --
      Bjorn Brox

      Comment

      • Peter Morris

        #4
        Re: how to convert this

        >got a string a="2.3944864845 405107E+18"
        Secondly: Converting a string to a double is language dependent, - in
        German, Norwegian and French as an example the expected decimal
        separator is a comma ',', instead of a period (.).
        I'm curious as to what will happen if you try converting this on a German
        locale as it is a number in standard form and and wonder if it is an
        exception or not. Could you try?


        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: how to convert this

          Peter Morris wrote:
          >>got a string a="2.3944864845 405107E+18"
          >Secondly: Converting a string to a double is language dependent, - in
          >German, Norwegian and French as an example the expected decimal
          >separator is a comma ',', instead of a period (.).
          >
          I'm curious as to what will happen if you try converting this on a German
          locale as it is a number in standard form and and wonder if it is an
          exception or not. Could you try?
          You can try it yourself just specifying a german culture as second
          argument.

          Arne

          Comment

          • Bjorn Brox

            #6
            Re: how to convert this

            Peter Morris skrev:
            >>got a string a="2.3944864845 405107E+18"
            >
            >Secondly: Converting a string to a double is language dependent, - in
            >German, Norwegian and French as an example the expected decimal
            >separator is a comma ',', instead of a period (.).
            >
            I'm curious as to what will happen if you try converting this on a German
            locale as it is a number in standard form and and wonder if it is an
            exception or not. Could you try?
            >
            >
            Convert.ToDoubl e(a) throw a System.FormatEx ception

            --
            Bjorn Brox

            Comment

            Working...