Parsing doubles in vb.NET

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

    Parsing doubles in vb.NET

    Hi,

    I'm having a problem parsing strings (comming from a flat text input file)
    to doubles.

    the code:
    currentImportDe tail.Result = CType(line.Subs tring(7, 8).Trim(" "),
    System.Double)

    What is in my Watch:
    line.Substring( 7, 8).Trim(" ") "0.006000"
    CType(line.Subs tring(7, 8).Trim(" "), System.Double) 6000.0
    System.Decimal. Parse(line.Subs tring(7, 8).Trim(" ")) 6000D
    cDec(line.Subst ring(7, 8).Trim(" ")) 6000D
    cdbl(line.Subst ring(7, 8).Trim(" ")) 6000.0

    I tried to use the other parse methods but for an unknown reason the result
    is false and I don't know why.

    My regional settings are currently Dutch(Belgium), but since he parses the
    string to 6000 point 0 and not comma, I doubt that the regional settings are
    the reason why it isn't parsing my data like I want.

  • SL33PY

    #2
    RE: Parsing doubles in vb.NET

    For anyone having the same problem:
    the solution:

    try using : line.Substring( 7, 8).Trim(" ").Replace(".", ",")

    Kind regards,
    SL33PY

    "SL33PY" wrote:
    [color=blue]
    > Hi,
    >
    > I'm having a problem parsing strings (comming from a flat text input file)
    > to doubles.
    >
    > the code:
    > currentImportDe tail.Result = CType(line.Subs tring(7, 8).Trim(" "),
    > System.Double)
    >
    > What is in my Watch:
    > line.Substring( 7, 8).Trim(" ") "0.006000"
    > CType(line.Subs tring(7, 8).Trim(" "), System.Double) 6000.0
    > System.Decimal. Parse(line.Subs tring(7, 8).Trim(" ")) 6000D
    > cDec(line.Subst ring(7, 8).Trim(" ")) 6000D
    > cdbl(line.Subst ring(7, 8).Trim(" ")) 6000.0
    >
    > I tried to use the other parse methods but for an unknown reason the result
    > is false and I don't know why.
    >
    > My regional settings are currently Dutch(Belgium), but since he parses the
    > string to 6000 point 0 and not comma, I doubt that the regional settings are
    > the reason why it isn't parsing my data like I want.
    >[/color]

    Comment

    • Robson

      #3
      Re: Parsing doubles in vb.NET

      Hold up a second, you are wanting to parse a double? (sorry if I
      misunderstood),

      You can:

      Double.TryParse (theString)

      or

      Try

      Double.Parse(th eString)

      Catch theException as SomeExceptionCa ntRemember

      End Try


      Hope this helps.



      "SL33PY" <SL33PY@discuss ions.microsoft. com> wrote in message
      news:B578AD49-A36D-451B-883D-FC2244935186@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I'm having a problem parsing strings (comming from a flat text input file)
      > to doubles.
      >
      > the code:
      > currentImportDe tail.Result = CType(line.Subs tring(7, 8).Trim(" "),
      > System.Double)
      >
      > What is in my Watch:
      > line.Substring( 7, 8).Trim(" ") "0.006000"
      > CType(line.Subs tring(7, 8).Trim(" "), System.Double) 6000.0
      > System.Decimal. Parse(line.Subs tring(7, 8).Trim(" ")) 6000D
      > cDec(line.Subst ring(7, 8).Trim(" ")) 6000D
      > cdbl(line.Subst ring(7, 8).Trim(" ")) 6000.0
      >
      > I tried to use the other parse methods but for an unknown reason the
      > result
      > is false and I don't know why.
      >
      > My regional settings are currently Dutch(Belgium), but since he parses the
      > string to 6000 point 0 and not comma, I doubt that the regional settings
      > are
      > the reason why it isn't parsing my data like I want.
      >[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Parsing doubles in vb.NET

        Sl33PY,

        Can you give us an idea what the string can looks like?

        However, I see that you are using a string with a dot as comma seperator.

        AFAIK, is that in the Dutch language for all three cultures a comma.

        Dim dbl As Double = CDbl("abcd100,1 0".Substring (4, 5))

        This gives for me 100.1

        Cor

        "SL33PY" <SL33PY@discuss ions.microsoft. com> schreef in bericht
        news:B578AD49-A36D-451B-883D-FC2244935186@mi crosoft.com...[color=blue]
        > Hi,
        >
        > I'm having a problem parsing strings (comming from a flat text input file)
        > to doubles.
        >
        > the code:
        > currentImportDe tail.Result = CType(line.Subs tring(7, 8).Trim(" "),
        > System.Double)
        >
        > What is in my Watch:
        > line.Substring( 7, 8).Trim(" ") "0.006000"
        > CType(line.Subs tring(7, 8).Trim(" "), System.Double) 6000.0
        > System.Decimal. Parse(line.Subs tring(7, 8).Trim(" ")) 6000D
        > cDec(line.Subst ring(7, 8).Trim(" ")) 6000D
        > cdbl(line.Subst ring(7, 8).Trim(" ")) 6000.0
        >
        > I tried to use the other parse methods but for an unknown reason the
        > result
        > is false and I don't know why.
        >
        > My regional settings are currently Dutch(Belgium), but since he parses the
        > string to 6000 point 0 and not comma, I doubt that the regional settings
        > are
        > the reason why it isn't parsing my data like I want.
        >[/color]


        Comment

        • Armin Zingler

          #5
          Re: Parsing doubles in vb.NET

          "SL33PY" <SL33PY@discuss ions.microsoft. com> schrieb[color=blue]
          > Hi,
          >
          > I'm having a problem parsing strings (comming from a flat text input
          > file) to doubles.
          >
          > the code:
          > currentImportDe tail.Result = CType(line.Subs tring(7, 8).Trim(" "),
          > System.Double)
          >
          > What is in my Watch:
          > line.Substring( 7, 8).Trim(" ") "0.006000"
          > CType(line.Subs tring(7, 8).Trim(" "), System.Double) 6000.0
          > System.Decimal. Parse(line.Subs tring(7, 8).Trim(" ")) 6000D
          > cDec(line.Subst ring(7, 8).Trim(" ")) 6000D
          > cdbl(line.Subst ring(7, 8).Trim(" ")) 6000.0
          >
          > I tried to use the other parse methods but for an unknown reason the
          > result is false and I don't know why.
          >
          > My regional settings are currently Dutch(Belgium), but since he
          > parses the string to 6000 point 0 and not comma,[/color]

          What do you mean with this? The IDE always shows as ".", not a ",", no
          matter where you live. This does not mean that it recognizes the "." as a
          decimal separator.

          [color=blue]
          > I doubt that the
          > regional settings are the reason why it isn't parsing my data like I
          > want.[/color]


          I think it is culture dependent.

          Try this instead:

          dim s as string

          s = line.Substring( 7, 8).Trim
          currentImportDe tail.Result = double.parse( _
          s, System.Globaliz ation.CultureIn fo.InvariantCul ture.NumberForm at) _
          )



          Armin

          Comment

          • Armin Zingler

            #6
            Re: Parsing doubles in vb.NET

            "SL33PY" <SL33PY@discuss ions.microsoft. com> schrieb[color=blue]
            > For anyone having the same problem:
            > the solution:
            >
            > try using : line.Substring( 7, 8).Trim(" ").Replace(".", ",")[/color]


            You know that this code doesn't work region independent? On a machine with
            English regional settings, the code would fail because you rely on the
            regional settings of the current machine whereas you have a region
            independent setting (English) in the text file.


            Armin

            Comment

            • Armin Zingler

              #7
              Re: Parsing doubles in vb.NET

              "Armin Zingler" <az.nospam@free net.de> schrieb[color=blue]
              > "SL33PY" <SL33PY@discuss ions.microsoft. com> schrieb[color=green]
              > > For anyone having the same problem:
              > > the solution:
              > >
              > > try using : line.Substring( 7, 8).Trim(" ").Replace(".", ",")[/color]
              >
              >
              > You know that this code doesn't work region independent? On a
              > machine with English regional settings, the code would fail because
              > you rely on the regional settings of the current machine whereas you
              > have a region independent setting (English) in the text file.[/color]


              Correction:
              ....you have a region *dependent* setting (English) in the text file.



              Armin

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Parsing doubles in vb.NET

                Armin,
                [color=blue]
                > s = line.Substring( 7, 8).Trim
                > currentImportDe tail.Result = double.parse( _
                > s, System.Globaliz ation.CultureIn fo.InvariantCul ture.NumberForm at) _
                > )[/color]

                The InvariantCultur e is the same culture as 'en-*' in my opinion not really
                usefull here, it makes the program again culture dependend because in AFAIK
                all English language cultures the dot is the comma seperator.

                Cor


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Parsing doubles in vb.NET

                  Armin,

                  With the same reply as you do it yourself, in this case it would work,
                  however that does the solution from the OP as well.

                  Cor


                  Comment

                  • Armin Zingler

                    #10
                    Re: Parsing doubles in vb.NET

                    "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb[color=blue]
                    > Armin,
                    >
                    > With the same reply as you do it yourself, in this case it would
                    > work, however that does the solution from the OP as well.[/color]

                    ??
                    The format in the text file is always English. InvariantCultur e is always
                    English, too. Thus, it *always* works. The OP's solution not always works.


                    Armin

                    Comment

                    • Armin Zingler

                      #11
                      Re: Parsing doubles in vb.NET

                      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb[color=blue]
                      > Armin,
                      >[color=green]
                      > > s = line.Substring( 7, 8).Trim
                      > > currentImportDe tail.Result = double.parse( _
                      > > s,
                      > > System.Globaliz ation.CultureIn fo.InvariantCul ture.NumberForm at) _
                      > > )[/color]
                      >
                      > The InvariantCultur e is the same culture as 'en-*' in my opinion not
                      > really usefull here, it makes the program again culture dependend[/color]

                      Yes, but it has to be culture dependend.
                      [color=blue]
                      > because in AFAIK all English language cultures the dot is the comma
                      > seperator.[/color]

                      That's why InvariantCultur e is useful because the text file always uses the
                      dot.

                      BTW, "the dot is the comma separator" is really nice. :-)


                      Armin

                      Comment

                      • Robson

                        #12
                        Re: Parsing doubles in vb.NET

                        I don't think you can have it both ways. You can't ask the program to run
                        in many varied cultures and yet import correctly formats from different
                        cultures again. This numeric information, if it's being used
                        across-cultures, needs to carry it's culture around with it, so you can
                        choose the correct culture to use for parsing. Either that or as Armin
                        says, you need to standardize on the invariant culture whereever your
                        software is being used.

                        "Armin Zingler" <az.nospam@free net.de> wrote in message
                        news:e66fe7M9FH A.1028@TK2MSFTN GP11.phx.gbl...[color=blue]
                        > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb[color=green]
                        >> Armin,
                        >>[color=darkred]
                        >> > s = line.Substring( 7, 8).Trim
                        >> > currentImportDe tail.Result = double.parse( _
                        >> > s,
                        >> > System.Globaliz ation.CultureIn fo.InvariantCul ture.NumberForm at) _
                        >> > )[/color]
                        >>
                        >> The InvariantCultur e is the same culture as 'en-*' in my opinion not
                        >> really usefull here, it makes the program again culture dependend[/color]
                        >
                        > Yes, but it has to be culture dependend.
                        >[color=green]
                        >> because in AFAIK all English language cultures the dot is the comma
                        >> seperator.[/color]
                        >
                        > That's why InvariantCultur e is useful because the text file always uses
                        > the
                        > dot.
                        >
                        > BTW, "the dot is the comma separator" is really nice. :-)
                        >
                        >
                        > Armin
                        >[/color]


                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Parsing doubles in vb.NET

                          Armin,

                          You are right, the OP solution will work in our cultures if it is either a
                          dot or a comma as decimal seperator.

                          The cultureInvarian t will forever work if the dot is used (en-*) as decimal
                          separator in every culture.

                          Cor


                          Comment

                          • Armin Zingler

                            #14
                            Re: Parsing doubles in vb.NET

                            "Robson" <nonono@nomeans no.com> schrieb[color=blue]
                            > I don't think you can have it both ways. You can't ask the program
                            > to run in many varied cultures and yet import correctly formats from
                            > different cultures again. This numeric information, if it's being
                            > used
                            > across-cultures, needs to carry it's culture around with it, so you
                            > can choose the correct culture to use for parsing. Either that or
                            > as Armin says, you need to standardize on the invariant culture
                            > whereever your software is being used.[/color]


                            I'm not sure which posting you are referring to. I don't see a problem here:
                            The application can be region independent and still read region dependent
                            text files. For example, parsing input in textboxes can be done with
                            double.parse w/o an explicit culture information. This uses the current
                            culture and makes the application and it's usage culture independent. The
                            same application reads from a text file. As a programmer, you must know the
                            format of the numbers in the file. If you know that it is English, the
                            invariant culture should be used to parse the values.


                            Armin

                            Comment

                            • Herfried K. Wagner [MVP]

                              #15
                              Re: Parsing doubles in vb.NET

                              "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb:[color=blue][color=green]
                              >> s = line.Substring( 7, 8).Trim
                              >> currentImportDe tail.Result = double.parse( _
                              >> s, System.Globaliz ation.CultureIn fo.InvariantCul ture.NumberForm at) _
                              >> )[/color]
                              >
                              > The InvariantCultur e is the same culture as 'en-*' in my opinion not
                              > really usefull here, it makes the program again culture dependend because
                              > in AFAIK all English language cultures the dot is the comma seperator.[/color]

                              Well, but it's necessary to use a single, non-ambiguous format when
                              exchanging data in text files.

                              --
                              M S Herfried K. Wagner
                              M V P <URL:http://dotnet.mvps.org/>
                              V B <URL:http://classicvb.org/petition/>

                              Comment

                              Working...